简体   繁体   中英

How to make Java JPA to create entities?

I am working on school project, where I need to use JPA. I have some schema like that:

Customer.java

@Data
@Entity(name = "CUSTOMER")
public class Customer {

@Id
@Column(name = "CUSTOMER_ID", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private long customerId;

@Column(name = "NAME")
private String name;

@Column(name = "SURNAME")
private String surname;

@Column(name = "STREET")
private String street;

@Column(name = "CITY")
private String city;

@Column(name = "ZIP_CODE")
private String zipCode;

@Column(name = "COUNTRY")
private String country;

@OneToMany(mappedBy="customer",targetEntity=Order.class, fetch= FetchType.EAGER)
private Collection orders;
}

And Order.java

@Data
@Entity(name = "ORDER")
public class Order {

@Id
@Column(name = "ORDER_ID", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private long orderId;

@Column(name = "CUSTOMER_ID")
@ManyToOne(optional=false)
@JoinColumn(name="CUSTOMER_ID",referencedColumnName="CUSTOMER_ID")
private long customerId;

@Column(name = "DATE_SHIPPED")
private Boolean dateShipped;

@Column(name = "ORDER_DATE")
private Date orderDate;

@Column(name = "STATUS")
private Date status;

}

Now, I've defined persistance.xml :

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
    <persistence-unit name="todos" transaction-type="RESOURCE_LOCAL">
        <class>b.model.Customer</class>
        <class>b.model.Order</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
            <property name="javax.persistence.jdbc.url"
                      value="jdbc:derby://localhost:1527/baza;create=true" />


        </properties>

    </persistence-unit>
</persistence>

And now I am stuck at the place, where I have no idea how to initialize the EntityManager to actually read my persistance.xml to connect to my database, and create the given entities. I've tried the following:

private static EntityManagerFactory factory;

public static void main(String[] args) {
    factory = Persistence.createEntityManagerFactory(....);
    EntityManager em = factory.createEntityManager();
}

But to be honest, I don't really know what should go into the constructor of the createEntityManagerFactor() method.

Any help is more than welcome!

What you need is specifying the persistance-unit name as you are creating the EntityManagerFactory :

factory = Persistence.createEntityManagerFactory("todos");
EntityManager em = factory.createEntityManager();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM