1

I am very new to hibernate and I am working with JPA and Hibernate4. Trying to insert parent object in child as onetoone relationship.

I went through some tutorials but All the example in the web shows, inserting both parent and child tables.

I want to insert data in child table only.

I have two tables called user and department.

User table consists of user details with department as onetoone relationship, as follows,

@Entity
@Table(name = "User")
public class UserEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "_id")
    private String id;

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

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

    @OneToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "departmentId")
    private Department departmentId;

    // getters and setters...

}

Below is my Department entity,

@Entity
@Table(name = "Department")
public class Department {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "_id")
    private String id;

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

    // getters and setters...
}

In department table there is only 4 data. I want to insert data only in user data while insert into it and don't want to insert in Department.

How can I do that.Please assist.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
J Query
  • 305
  • 4
  • 19

3 Answers3

1

You need to specify the relationship owner using mappedBy property in the OneToOne mapping in the owner side, here in your case in the Department class, you should add:

@OneToOne(mappedBy="department")
private UserEntity user;

I updated your code, to included the stated annotation and also renamed the Department property in your UserEntity class from departmentId to department to avoid confusion between relationship owner and its id:

@Entity
@Table(name = "User")
public class UserEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "_id")
    private String id;

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

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

    @OneToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "departmentId")
    private Department department;

    // getters and setters...

}

Below is the Department entity,

@Entity
@Table(name = "Department")
public class Department {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "_id")
    private String id;

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

    @OneToOne(mappedBy="department")
    private UserEntity user;

    // getters and setters...
}

This will give you the right mapping with the expected behaviour.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
1

In the @OneToOne annotation, the default value for parameter optional is true. So your annotation is the same as @OneToOne(fetch = FetchType.EAGER, optional = true). This means you can simply leave the Department in a UserEntity instance empty. In that case, persisting it results in persisting only a user entity and no department.

Even if you created a Department instance and assigned it to a UserEntity instance, persisting the UserEntity would not automatically persist the Department, since you don't have any cascade parameter in your annotation. If you don't automatically cascade persists, you would have to persist the Department first and then persist the corresponding user entity.

Maybe you're asking about using existing departments for your user entities. In that case, you first need to get the department via Hibernate (or the JPA API) from an entity manager. The entity instance you get is managed by Hibernate, and you can then set it in a UserEntity and persist that, to have it refer to the department.

Finally, I think one department will probably have more than one user. It might make more sense to have a @ManyToOne annotation instead of @OneToOne, indicating multiple users can refer to the same department, but that depends on your domain model.

Frank Pentangeli
  • 336
  • 2
  • 6
  • 20
G_H
  • 11,739
  • 3
  • 38
  • 82
1

You have to use mappedBy for this, as mentoned below in child Table, Department in your case

@OneToOne(mappedBy="department")
private UserEntity user;

These posts explain you better this,

JPA JoinColumn vs mappedBy

Understanding mappedBy annotation in Hibernate

DevGo
  • 1,045
  • 1
  • 16
  • 42