I have the following two entities:
Person.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;
}
}
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;
}
}
I would like to map these two entities to the following schema:
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
);
Note that the address table does not have a primary key, just a foreign key onto the person table. This means that an address cannot exist without a person associated with it and that the mapping must be one-to-one.
Unfortunately, the Hibernate annotations do not work correctly. This is the code I am using to test the above:
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(
}
But the error I get is:
org.hibernate.id.IdentifierGenerationException: null id generated for:class Address
I have tried many permutations of various different annotations without success. Any ideas how I can fix the annotations in the above code so that it correctly persists both Person and Address entities? The full code if you want to test it yourself is here along with instructions on how to set up the database:
I found the solution was to remove the “optional = false” attribute from the Person’s OneToOne annotation on the address field:
I believe this works because it prevents Hibernate from attempting to persist the Address before the id for the Person has been generated.
Please take a look at the updated github repository for a full working example: https://github.com/alexspurling/hibernate-test