您好我正在使用hibernate将数据保存到数据库的两个表中。另外,我使用的是mysql。
这些是我的POJO课程,
Patient.java
public class Patient implements java.io.Serializable {
private Integer idPatient;
private String title;
private String firstName;
private String lastName;
private String middleName;
private Date dob;
private Boolean martitalStatus;
private String gender;
private String nic;
private Date dateCreated;
private Date lastUpdated;
private Set<Contact> contacts = new HashSet<Contact>(0);
public Patient() {
}
public Patient(Date lastUpdated) {
this.lastUpdated = lastUpdated;
}
public Patient(String title, String firstName, String lastName, String middleName, Date dob, Boolean martitalStatus, String gender, String nic, Date dateCreated, Date lastUpdated, Set<Contact> contacts) {
this.title = title;
this.firstName = firstName;
this.lastName = lastName;
this.middleName = middleName;
this.dob = dob;
this.martitalStatus = martitalStatus;
this.gender = gender;
this.nic = nic;
this.dateCreated = dateCreated;
this.lastUpdated = lastUpdated;
this.contacts = contacts;
}
public Integer getIdPatient() {
return this.idPatient;
}
public void setIdPatient(Integer idPatient) {
this.idPatient = idPatient;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getMiddleName() {
return this.middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
public Date getDob() {
return this.dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public Boolean getMartitalStatus() {
return this.martitalStatus;
}
public void setMartitalStatus(Boolean martitalStatus) {
this.martitalStatus = martitalStatus;
}
public String getGender() {
return this.gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getNic() {
return this.nic;
}
public void setNic(String nic) {
this.nic = nic;
}
public Date getDateCreated() {
return this.dateCreated;
}
public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}
public Date getLastUpdated() {
return this.lastUpdated;
}
public void setLastUpdated(Date lastUpdated) {
this.lastUpdated = lastUpdated;
}
public Set<Contact> getContacts() {
return this.contacts;
}
public void setContacts(Set<Contact> contacts) {
this.contacts = contacts;
}
}
Contact.java
public class Contact implements java.io.Serializable {
private Integer idContact;
private Patient patient;
private String telephone;
private String address;
public Contact() {
}
public Contact(Patient patient) {
this.patient = patient;
}
public Contact(Patient patient, String telephone, String address) {
this.patient = patient;
this.telephone = telephone;
this.address = address;
}
public Integer getIdContact() {
return this.idContact;
}
public void setIdContact(Integer idContact) {
this.idContact = idContact;
}
public Patient getPatient() {
return this.patient;
}
public void setPatient(Patient patient) {
this.patient = patient;
}
public String getTelephone() {
return this.telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getAddress() {
return this.address;
}
public void setAddress(String address) {
this.address = address;
}
}
此外,我在这里没有使用注释。
Patient patient=new Patient();
Contact contact=new Contact();
Set<Contact> contacts=new HashSet<Contact>();
contact.setTelephone("0358965458");
contact.setAddress("Horana");
contacts.add(contact);
patient.setFirstName("Wajira");
patient.setLastName("Dahanushka");
patient.setDateCreated(Common.getSQLCurrentTimeStamp());
patient.setLastUpdated(Common.getSQLCurrentTimeStamp());
patient.setGender("Male");
patient.setTitle("Mr");
patient.setNic("9115580466v");
patient.setContacts(contacts);
SessionFactory sessionFactory=new HibernateUtil().getSessionFactory();
Session session=sessionFactory.openSession();
session.beginTransaction();
session.persist(patient);
session.getTransaction().commit();
HibernateUtil.shutdown();
Patient.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 8, 2016 9:56:01 AM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
<class name="db.Patient" table="patient" catalog="example_hibernate" optimistic-lock="version">
<id name="idPatient" type="java.lang.Integer">
<column name="idPatient" />
<generator class="identity" />
</id>
<property name="title" type="string">
<column name="title" length="45" />
</property>
<property name="firstName" type="string">
<column name="firstName" length="45" />
</property>
<property name="lastName" type="string">
<column name="lastName" length="45" />
</property>
<property name="middleName" type="string">
<column name="middleName" length="45" />
</property>
<property name="dob" type="date">
<column name="dob" length="10" />
</property>
<property name="martitalStatus" type="java.lang.Boolean">
<column name="martitalStatus" />
</property>
<property name="gender" type="string">
<column name="gender" length="45" />
</property>
<property name="nic" type="string">
<column name="nic" length="45" />
</property>
<property name="dateCreated" type="timestamp">
<column name="dateCreated" length="19" />
</property>
<property name="lastUpdated" type="timestamp">
<column name="lastUpdated" length="19" not-null="true" />
</property>
<set name="contacts" table="contact" inverse="true" lazy="true" fetch="select">
<key>
<column name="idPatient" not-null="true" />
</key>
<one-to-many class="db.Contact" />
</set>
</class>
</hibernate-mapping>
Contact.hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 8, 2016 9:56:01 AM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
<class name="db.Contact" table="contact" catalog="example_hibernate" optimistic-lock="version">
<id name="idContact" type="java.lang.Integer">
<column name="idContact" />
<generator class="identity" />
</id>
<many-to-one name="patient" class="db.Patient" fetch="select">
<column name="idPatient" not-null="true" />
</many-to-one>
<property name="telephone" type="string">
<column name="telephone" length="45" />
</property>
<property name="address" type="string">
<column name="address" length="45" />
</property>
</class>
</hibernate-mapping>
联系表有一个外键,它是患者表的主键。
当我运行上面的代码时,患者表中会看到一条新的患者记录,但在联系表中看不到任何新的联系记录。
有什么不对吗? 有任何想法。
答案 0 :(得分:3)
你需要告诉Hibernate,患者和他们的联系人之间存在一对多的关系。完成此操作后,持久保存Patient
对象也应该保留该患者的整个图表。如果您使用注释,那么这样的事情应该有效:
@Entity
@Table(name="Patient")
public class Patient {
@OneToMany(mappedBy="patient")
private Set<Contact> contacts;
}
@Entity
@Table(name="Contact")
public class Contact {
@ManyToOne
private Patient patient;
}
答案 1 :(得分:2)
在提及联系人的set
时,请使用以下级联属性
<set name="contacts" table="contact" cascade="save-update" inverse="true" lazy="true" fetch="select">
<key>
<column name="idPatient" not-null="true" />
</key>
<one-to-many class="db.Contact" />
</set>
通过提及级联为save-update
,您只需调用session.save(patientObj)
,联系人详细信息就会自动保留。
检查this链接,以获取hibernate关联中级联更新/插入/删除操作的完整参考。