我是 Hibernate 的新手,正在尝试一些应该很容易的事情,但我无法让它工作。
有两个表,一个人和一个地址。一个人可以有一个或多个地址,即:一个 OneToMany 映射。当我尝试将相同的地址添加给两个不同的人时,我得到了一个例外。几乎就好像对“连接”表内的 foriegn_key 强制执行唯一性一样。
我的源代码:
人
package testing.com.hibernate.entities;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name="Person")
public class Person {
private long personID;
private String personName;
private Set<Address> addresses = new HashSet<Address>(0);
public Person() {}
public Person(String personName, Set<Address> addresses) {
setPersonName(personName);
setAddresses(addresses);
}
@Id
@GeneratedValue(generator="increment")
@GenericGenerator(name="increment", strategy="increment")
@Column(name="Person_ID")
public long getPersonID() {
return personID;
}
@Column(name="Person_Name")
public String getPersonName() {
return personName;
}
@OneToMany
@JoinTable(name = "PersonAddresses", joinColumns = {@JoinColumn(name="PersonID", unique=false)}, inverseJoinColumns = {@JoinColumn(name="AddressID", unique=false)})
public Set<Address> getAddresses() {
return addresses;
}
public void setPersonID(long personID) {
this.personID = personID;
}
public void setPersonName(String personName) {
this.personName = personName;
}
public void setAddresses(Set<Address> addresses) {
this.addresses = addresses;
}
}
地址
package testing.com.hibernate.entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name="Address")
public class Address {
private long addressID;
private String address;
public Address() {}
public Address(String address) {
setAddress(address);
}
@Id
@GeneratedValue(generator="increment")
@GenericGenerator(name="increment", strategy="increment")
@Column(name="Address_ID")
public long getAddressID() {
return addressID;
}
@Column(name="Address")
public String getAddress() {
return address;
}
public void setAddressID(long addressID) {
this.addressID = addressID;
}
public void setAddress(String address) {
this.address = address;
}
}
主要的
package testing.com.hibernate.tests;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import testing.com.hibernate.entities.Address;
import testing.com.hibernate.entities.EntityCategory;
import testing.com.hibernate.entities.EntityNetflixFilm;
import testing.com.hibernate.entities.Person;
import testing.com.hibernate.sessionmanager.HibernateUtil;
import org.hibernate.Session;
public class Test {
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
// Create multiple addresses
Address addr1 = new Address("Address one");
Address addr2 = new Address("Address two");
Address addr3 = new Address("Address three");
Address addr4 = new Address("Address four");
// Add all addresses to the database
session.beginTransaction();
session.save(addr1);
session.save(addr2);
session.save(addr3);
session.save(addr4);
session.getTransaction().commit();
session.close();
/*
* At this point in the code there will be four addresses
* in the table 'Address'.
*
* 1 - Address one
* 2 - Address two
* 3 - Address three
* 4 - Address four
*
* Now we want to create some people and add addresses to them
*/
// Pull out a list of addresses from the database
List<Address> addresses = null;
session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
addresses = session.createQuery("from Address").list();
session.getTransaction().commit();
session.close();
/*
* We now have a List of Address objects.
*
* This is replicating a Database with existing
* Addresses and querying them from the table
* to attach to a new Person being added.
*/
// Create person with Address 1 and 2
Set<Address> addressSet1 = new HashSet<Address>();
addressSet1.add(addresses.get(0));
addressSet1.add(addresses.get(1));
Person george = new Person("George", addressSet1);
// Create person with Address 3 and 4
Set<Address> addressSet2 = new HashSet<Address>();
addressSet2.add(addresses.get(2));
addressSet2.add(addresses.get(3));
Person robert = new Person("Robert", addressSet2);
// Create person with Address 1 and 2. The same as George
Person harry = new Person("Harry", addressSet1);
// Attempt to add them to the Person table
session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
session.save(george);
session.save(robert);
session.save(harry);
session.getTransaction().commit();
session.close();
}
}
代码在保存 Person 对象“Harry”时崩溃。这是因为在 PersonAddress 表中,引用为“1”的地址已经存在并分配给了“George”。我怎样才能使'Harry'和'George'可以映射到地址'1'?
日志
Hibernate:插入 Person (Person_Name, Person_ID) 值 (?, ?) 22:39:42,284 TRACE BasicBinder:83 - 绑定参数 [1] 作为 [VARCHAR] - George 22:39:42,284 TRACE BasicBinder:83 - 绑定参数 [ 2] 作为 [BIGINT] - 1 休眠:插入到人(Person_Name,Person_ID)值(?,?) 22:39:42,285 TRACE BasicBinder:83 - 绑定参数 [1] 作为 [VARCHAR] - Robert 22:39:42,286 TRACE BasicBinder:83 - 绑定参数 [2] 作为 [BIGINT] - 2 Hibernate:插入人(Person_Name,Person_ID)值(?,?)22:39:42,287 TRACE BasicBinder:83 - 绑定参数 [1] 作为 [VARCHAR ] - Harry 22:39:42,287 TRACE BasicBinder:83 - 绑定参数 [2] 作为 [BIGINT] - 3
Hibernate:插入 PersonAddresses (PersonID, AddressID) 值 (?, ?) 22:39:42,289 TRACE BasicBinder:83 - 绑定参数 [1] 作为 [BIGINT] - 1 22:39:42,292 TRACE BasicBinder:83 - 绑定参数 [ 2] 作为 [BIGINT] - 1
Hibernate:插入 PersonAddresses (PersonID, AddressID) 值 (?, ?) 22:39:42,294 TRACE BasicBinder:83 - 绑定参数 [1] 作为 [BIGINT] - 1 22:39:42,295 TRACE BasicBinder:83 - 绑定参数 [ 2] 作为 [BIGINT] - 2
Hibernate:插入 PersonAddresses (PersonID, AddressID) 值 (?, ?) 22:39:42,296 TRACE BasicBinder:83 - 绑定参数 [1] 作为 [BIGINT] - 2 22:39:42,296 TRACE BasicBinder:83 - 绑定参数 [ 2] 作为 [BIGINT] - 3
Hibernate:插入 PersonAddresses (PersonID, AddressID) 值 (?, ?) 22:39:42,297 TRACE BasicBinder:83 - 绑定参数 [1] 作为 [BIGINT] - 2 22:39:42,298 TRACE BasicBinder:83 - 绑定参数 [ 2] 作为 [BIGINT] - 4
Hibernate:插入 PersonAddresses (PersonID, AddressID) 值 (?, ?) 22:39:42,298 TRACE BasicBinder:83 - 绑定参数 [1] 作为 [BIGINT] - 3 22:39:42,300 TRACE BasicBinder:83 - 绑定参数 [ 2] 作为 [BIGINT] - 1
22:39:42,320 WARN SqlExceptionHelper:143 - SQL 错误:1062,SQLState:23000 22:39:42,320 错误 SqlExceptionHelper:144 - 键 'AddressID' 的重复条目 '1'
线程“主”org.hibernate.exception.ConstraintViolationException 中的异常:键“AddressID”的重复条目“1”