Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8851817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:14:58+00:00 2026-06-14T13:14:58+00:00

I have the following two entities: Person.java: @Entity @Table(name=PERSON) public class Person { @Id

  • 0

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:

https://github.com/alexspurling/hibernate-test

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:14:59+00:00Added an answer on June 14, 2026 at 1:14 pm

    I found the solution was to remove the “optional = false” attribute from the Person’s OneToOne annotation on the address field:

    @OneToOne(cascade = CascadeType.ALL, mappedBy = "person")
    private Address address;
    

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two entities: @Entity public class File ....... @Id @GeneratedValue(strategy=GenerationType.AUTO) private int id;
I have the following two entities: @Entity public class SupermarketChain { @Id @GeneratedValue(strategy =
We have the following two entities with many-to-many association: @Entity public class Role {
I have two entities like the following: @Entity public class Trip { @OneToMany(mappedBy =
I have two entities Room.php /** * @ORM\Table(name=room) * @ORM\Entity(repositoryClass=Ahsio\StackBundle\Repository\RoomRepository) */ class Room {
I have the following two entities: @Entity class Relation{ @ManyToOne private User user; //some
I have two entities, defined as following public class Corporation { public int Id{get;set;}
I have following two entities public class User { [Key] public int Id {
I have the following two entities: public class Entity1 { public IList e2 {
I have following two entities public class User { public int UserId { get;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.