我有以下两个实体:
人.java:
@Entity
@Table(name="PERSON")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id")
private int id;
@Column(name="first_name")
private String firstName;
@Column(name="last_name")
private String lastName;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "person", optional = false)
private Address address;
public Person(final String firstName, final String lastName, final Address address)
{
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
}
//Required by hibernate
public Person()
{
}
public int getId()
{
return id;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public Address getAddress() {
return address;
}
public void setId(final int id)
{
this.id = id;
}
public void setFirstName(final String firstName)
{
this.firstName = firstName;
}
public void setLastName(final String lastName)
{
this.lastName = lastName;
}
public void setAddress(final Address address)
{
this.address = address;
}
}
地址.java:
@Entity
@Table(name = "ADDRESS")
public class Address
{
@Id
private int personId;
@MapsId
@OneToOne()
private Person person;
@Column(name = "street")
private String street;
@Column(name = "town")
private String town;
@Column(name = "postcode")
private String postcode;
public Address()
{
}
public Address(final String street, final String town, final String postcode)
{
this.street = street;
this.town = town;
this.postcode = postcode;
}
public Person getPerson()
{
return person;
}
public void setPerson(final Person person)
{
this.person = person;
this.personId = person.getId();
}
public String getStreet()
{
return street;
}
public void setStreet(final String street)
{
this.street = street;
}
public String getTown()
{
return town;
}
public void setTown(final String town)
{
this.town = town;
}
public String getPostcode()
{
return postcode;
}
public void setPostcode(final String postcode)
{
this.postcode = postcode;
}
}
我想将这两个实体映射到以下架构:
CREATE TABLE person (
id INT NOT NULL AUTO_INCREMENT,
first_name VARCHAR(50) NULL DEFAULT NULL,
last_name VARCHAR(50) NULL DEFAULT NULL,
PRIMARY KEY (id)
);
CREATE TABLE address (
person_id INT(10) NOT NULL,
street VARCHAR(50) NULL DEFAULT NULL,
town VARCHAR(50) NULL DEFAULT NULL,
postcode VARCHAR(50) NULL DEFAULT NULL,
FOREIGN KEY (person_id) REFERENCES person(id) ON DELETE CASCADE
);
请注意,地址表没有主键,只有人员表的外键。这意味着地址不能在没有与之关联的人的情况下存在,并且映射必须是一对一的。
不幸的是,Hibernate 注释不能正常工作。这是我用来测试上述内容的代码:
public static Person save(Person person) {
SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
Integer id = (Integer)session.save(person);
person.setId(id);
session.getTransaction().commit();
session.close();
return person;
}
@Test
public void testWritePerson() {
Person person = new Person("Jack", "Bauer", new Address("123 Fake Street", "Springfield", "SP1F 123"));
person.getAddress().setPerson(person);
Person savedPerson = PersonTestUtil.save(person);
assertFalse(
}
但我得到的错误是:
org.hibernate.id.IdentifierGenerationException: null id generated for:class Address
我尝试了各种不同注释的许多排列,但没有成功。有什么想法可以修复上述代码中的注释,以便正确保留 Person 和 Address 实体吗?如果您想自己测试它的完整代码以及如何设置数据库的说明,请参见此处: