帖子很长但是因为我想解释一切足以让你帮助我的事情。我创建了两个表,我想使用hibernate创建一对多关系(每个客户可以有很多订单)。我创建这个表的方式如下所示:
create table if not exists customer(
customer_id int not null auto_increment,
first_name varchar(50),
last_name varchar(50),
primary key (customer_id)
);
create table if not exists orders(
order_id int not null auto_increment,
order_date timestamp not null default current_timestamp,
adress varchar(50),
customer int not null,
primary key (order_id),
foreign key (customer) references customer(customer_id)
);
现在我想创建java项目并使用hibernate将该项目与我的数据库连接。
pom.xml中的依赖项:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.15</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.3.Final</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>
hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="application.hbm.xml" />
</session-factory>
</hibernate-configuration>
application.hbm.xml,其中我在实体和数据库列之间创建连接:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="entity.Customer" table="customer">
<id name="idCustomer" column="customer_id">
<generator class="increment" />
</id>
<property name="firstName" column="first_name" />
<property name="lastName" column="last_name" />
<set name="orders" inverse="true" cascade="persist" >
<key column="order_id" />
<one-to-many class="entity.Order" />
</set>
</class>
<class name="entity.Order" table="orders">
<id name="idOrder" column="order_id">
<generator class="increment"></generator>
</id>
<property name="orderDate" column="order_date"></property>
<property name="adress" column="adress"></property>
<many-to-one name="customer" column="customer" not-null="true" />
</class>
</hibernate-mapping>
客户实体:
package entity;
import java.util.Set;
public class Customer {
private long idCustomer;
private String firstName;
private String lastName;
private Set<Order> orders;
public long getIdCustomer() {
return idCustomer;
}
public void setIdCustomer(long idCustomer) {
this.idCustomer = idCustomer;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Set<Order> getOrders() {
return orders;
}
public void setOrders(Set<Order> orders) {
this.orders = orders;
}
@Override
public String toString() {
String returnString = "Customer [firstName=" + firstName + ", lastName=" + lastName + "]";
returnString += "\nOrders: ";
for(Order order : orders) {
returnString = returnString +"\n" + order;
}
return returnString;
}
}
订单实体:
package entity;
import java.util.Date;
public class Order {
private int idOrder;
private Date orderDate;
private String adress;
private Customer customer;
public int getIdOrder() {
return idOrder;
}
public void setIdOrder(int idOrder) {
this.idOrder = idOrder;
}
public Date getOrderDate() {
return orderDate;
}
public void setOrderDate(Date orderDate) {
this.orderDate = orderDate;
}
public String getAdress() {
return adress;
}
public void setAdress(String adress) {
this.adress = adress;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
@Override
public String toString() {
return "Order [idOrder=" + idOrder + ", orderDate=" + orderDate + ", adress=" + adress + "]";
}
}
直到这一刻,一切正常,当我在main方法中运行这样的代码时:
Configuration conf = new Configuration().configure();
SessionFactory factory = conf.buildSessionFactory();
Session session = factory.openSession();
Customer customer = (Customer) session.get(Customer.class, 1l);
Transaction transaction = session.beginTransaction();
Order order = new Order();
order.setAdress("plac strzelecki 212");
order.setOrderDate(new Date());
order.setCustomer(customer);
customer.getOrders().add(order);
session.save(customer);
transaction.commit();
session.close();
订单保存在数据库中。但在尝试加载此客户并显示其订单后,代码显示客户没有订单...例如,当我这样做时:
Configuration conf = new Configuration().configure();
SessionFactory factory = conf.buildSessionFactory();
Session session = factory.openSession();
Customer customer = (Customer) session.get(Customer.class, 1l);
System.out.println(customer.getOrders().size());
session.close();
输出为0 ......我做错了什么?或者我不理解的是什么?