我在hibernate项目中有以下文件:
Customer.java,Request.java,Client.java和hibernate.cfg.xml如下:
Customer.java
@Entity
@Table(name="customers")
public class Customer {
@Id
@Column(name="cid")
@GeneratedValue(strategy=GenerationType.AUTO)
private int cid;
@Column(name="name")
private String name;
@Column(name="phone")
private long phone;
@OneToMany(mappedBy="customer")
private Set<Request> requests;
public Customer() {
super();
}
public Customer(String name, long phone) {
super();
this.name = name;
this.phone = phone;
}
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getPhone() {
return phone;
}
public void setPhone(long phone) {
this.phone = phone;
}
public Set<Request> getRequests() {
return requests;
}
public void setRequests(Set<Request> requests) {
this.requests = requests;
}
@Override
public String toString() {
return "Customer [cid=" + cid + ", name=" + name + ", phone=" + phone
+"]";
}
}
Request.java
@Entity
@Table(name="requests")
public class Request {
@Id
@Column(name="reqid")
@GeneratedValue(strategy=GenerationType.AUTO)
private int reqId;
@Column(name="reqdate")
private String reqDate;
@Column(name="description")
private String description;
@Column(name="status")
private String status;
@ManyToOne
@JoinColumn(name="cid",referencedColumnName="cid")
private Customer customer;
public Request() {
super();
}
public Request(String reqDate, String description, String status) {
super();
this.reqDate = reqDate;
this.description = description;
this.status = status;
}
public int getReqId() {
return reqId;
}
public void setReqId(int reqId) {
this.reqId = reqId;
}
public String getReqDate() {
return reqDate;
}
public void setReqDate(String reqDate) {
this.reqDate = reqDate;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
@Override
public String toString() {
return "Request [reqId=" + reqId + ", reqDate=" + reqDate
+ ", description=" + description + ", status=" + status
+ ", customer=" + customer + "]";
}
}
的hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//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:3306/ahamdb</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping class="com.kar.hibernate.Customer" />
<mapping class="com.kar.hibernate.Request" />
</session-factory>
</hibernate-configuration>
Client.java
public class Client{
public static void main(String[] args) {
Transaction tx=null;
try
{
SessionFactory sessionFactory=AHibernateUtil.getSessionFactory();
Session session=sessionFactory.openSession();
tx=session.beginTransaction();
Customer cust=new Customer("bnuj",1111);
session.save(cust);
Request req1=new Request("4-1-14", "desc1", "active");
session.save(req1);
Request req2=new Request("4-2-14", "desc2", "unactive");
session.save(req2);
Set<Request> requests=new HashSet<Request>();
requests.add(req1);
requests.add(req2);
cust.setRequests(requests);
tx.commit();
session.close();
}
catch(Exception e)
{
e.printStackTrace();
if(tx!=null)
tx.rollback();
}
}
}
问题是,当我运行客户端代码时,它产生的结果为:
Hibernate: insert into customers (name, phone) values (?, ?)
Hibernate: insert into requests (cid, description, reqdate, status) values (?, ?, ?, ?)
Hibernate: insert into requests (cid, description, reqdate, status) values (?, ?, ?, ?)
它没有更新请求表中的外键列,我不明白为什么会这样 有人可以帮忙吗?
我想知道我是否正确地做了,如果没有,是否有人可以发布正确的解决方案?
答案 0 :(得分:1)
问题来自逆映射。如果我们声明:@OneToMany(mappedBy="customer")
,即 mappedBy
,我们会指示Hibernate:
关系的另一端将关心持久性。
如果另一端 - 请求,将会知道它,它将会正确地分配给客户。所以这应该解决这个问题:
...
cust.setRequests(requests);
req1.setCustomer(cust);
req2.setCustomer(cust);
Hibernate现在有足够的信息来正确插入/更新关系
这个阅读也应该有所帮助: