0

I have two tables I need to insert in to in Hibernate - I have a User and every user belongs is a Member. Therfore when creating a new user I need a new entry in the Member table. I have attempted this via creating a Member object which maps to my member table and then having that as a field in my User object which maps to the user table

@Entity
@Table(name = "USER")
public class User
{
    @Id
    @GeneratedValue
    @Column(name = "id")
    private int id;

    @Column(name = "username")
    private String username;

    @Column(name = "password")
    private String password;

    @Column(name = "fullName")
    private String fullName;

    //other fields ommited

    @OneToOne
    @JoinColumn(name = "id")
    private Member member;

My member pojo looks as follows

@Entity
@Table(name = "MEMBER")
public class Member
{
    @Id
    @GeneratedValue
    @Column(name = "id")
    private int id;

    @Column(name = "sgpid")
    private int sgpid;

    @Column(name = "username")
    private String username;

Trying to save the object i do as follows;

@Override
public boolean addUser(User user)
{
    if (user == null)
    {
        throw new IllegalArgumentException("Unable to add null user");
    }

    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    session.save(user);
    session.getTransaction().commit();

    return true;
}

This gives me the row saved in my user table but the entry is not inserted in to the member table. I think my linking annotations are probably incorrect but I am not too sure - please could someone provide some assistance.

Thanks

Biscuit128
  • 5,218
  • 22
  • 89
  • 149
  • 1
    `@JoinColumn(name = "id")` would indicate that users and members share the same id. Are you sure that your column shouldn't be something like "member_id" ? This way, every user has a member_id that points to it's member. – Florian Schaetz Aug 16 '16 at 10:31
  • the user and member link via the two id's but i might actually change that now to link on the username – Biscuit128 Aug 16 '16 at 10:37
  • Possible duplicate of [How to import initial data to database with Hibernate?](http://stackoverflow.com/questions/673802/how-to-import-initial-data-to-database-with-hibernate) – Grim Aug 16 '16 at 10:54

2 Answers2

1

Try to set the cascade value of the @OneToOne annotation:

@OneToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "id")
private Member member;
Sándor Juhos
  • 1,535
  • 1
  • 12
  • 19
  • i realised the document for the database that i am trying to write too is out of date and actually uses the username to do the linking between the two tables - does this just require a change to the join columnm annotation to use the username instead? – Biscuit128 Aug 16 '16 at 10:39
  • if i do change that to username then i get the following error `org.hibernate.MappingException: Repeated column in mapping for entity: com.rest.dao.model.User column: username (should be mapped with insert="false" update="false")` – Biscuit128 Aug 16 '16 at 10:41
1

First thing in your user class you should change the joinColumn to member_id.

As mentioned in another answer to persist a related entity you need to set the cascade to persist, i would recommend using cascade All which will involve the related entity in all operations check the doc https://docs.oracle.com/cd/E19798-01/821-1841/bnbqm/index.html

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "member_id")
private Member member;
Amer Qarabsa
  • 6,412
  • 3
  • 20
  • 43
  • the foreign key is expecting the username - every time i use that i get the following error `org.hibernate.Mappi‌​ngException: Repeated column in mapping for entity: com.rest.dao.model.U‌​ser column: username (should be mapped with insert="false" update="false")` – Biscuit128 Aug 16 '16 at 13:53
  • I guess you are using username name for the join column , this will make duplicate with the username of the User entity , instead of using the default which is the name of the attribute in member class "username" , use another name @JoinColumn(name="member_username",referencedColumnName="username") – Amer Qarabsa Aug 16 '16 at 15:22
  • so the join needs to go on the member username column rather than the user? – Biscuit128 Aug 16 '16 at 15:42
  • No the join still in User entity like this @OneToOne(cascade = CascadeType.ALL) @JoinColumn(name="me‌​mber_username",refere‌​ncedColumnName="usern‌​ame") private Member member; – Amer Qarabsa Aug 16 '16 at 18:39