0

I'm have a problem with sql created by Hibernate when I use entity mapped shared primary key. I'm using JPA 2.1 and Hibernate 5.2.2

Here's my entities:

@Entity
@Column(name = "employee_table")
public class EmployeeEntity {

    @Id
    @Column(name = "id")
    @SequenceGenerator
    @GeneratedValue
    private Long id;

    @OneToOne(fetch = FetchType.LAZY)
    @PrimaryKeyJoinColumn
    private EmployeeDetailsEntity employeeDetailsEntity;
}

@Entity
@Table(name = "employee_details")
public class EmployeeDetailsEntity {

    @Id
    @Column(name = "id")
    private Long id;

    // additional attributes

}

I want to select all employees which has details:

select e from EmployeeEntity e where e.employeeDetailsEntity is not null;

Select that was generated by Hibernate is:

select employeeen0_.id from employee_table employeeen0_ where employeeen0_.id is not null;

Could you please explaine me what I'm doing wrong and help to solve this?

Dmytro
  • 83
  • 1
  • 7
  • What JPA version are you using? – dcalap Oct 18 '16 at 13:27
  • Added info abount JPA and Hibernate version. – Dmytro Oct 18 '16 at 13:31
  • Have you checked this link? http://stackoverflow.com/a/6839336/1230748 – dcalap Oct 18 '16 at 13:37
  • Yea. But unfortunatelly can't understand why I got such native sql. – Dmytro Oct 18 '16 at 14:19
  • I don't know where is exactly the error, but i can recommend you few things, the table just call it "Employee" and the detail table: "EmployDetail" don't use "_" for code convention on Tables name its UpperCamelCase and on the attributes name its camelCase. I have had bugs when use the "id" or "pk" as Long, I always use it like Integer, and the relations should be @ManyToOne/@OneToMany. One Employee can have Many Details, or Many Employee can have One Detail, try it with this relational and see what happen. – Ramiro Arizpe Giacomelli Oct 18 '16 at 14:29
  • @R.A actually none of your suggestions make sense: 1. it is common to have different naming convention in DB. Using same naming convention as code does not necessary make things easier to maintain. 2. Using Long as ID has never been a problem for my past projects. Please give evidence or share your experience on how it can be a problem 3. Whether it is OneToMany or OneToOne totally depends on OP's data model. Why it have to be OneToMany? – Adrian Shum Oct 20 '16 at 06:39

2 Answers2

1

It looks like you're missing the "other side" of the Employee -> EmployeeDetails relationship mapping:

Employee entity:

private EmployeeDetails employeeDetails;

    @OneToOne(mappedBy = "employee", fetch = FetchType.LAZY)
    public EmployeeDetails getEmployeeDetails() {
        return employeeDetails;
    }

EmployeeDetails entity:

private Employee employee;

    @OneToOne(optional = false)
    @JoinColumn(name = "EMPLOYEE_ID")
    public Employee getEmployee() {
        return employee;
    }
egallardo
  • 1,234
  • 1
  • 15
  • 25
0

Thanks to all who comments. The problem is that @PrimaryKeyJoinColumn on EmployeeDetailsEntity using EmployeeEntity primary key and when in your hql you check that EmployeeDetailsEntity in null (or not null) for native sql Hibernate use id of EmployeeEntity because you are shared this PK with another (EmployeeDetailsEntity) entity.

If you don't want (can't) to change you mapping use join in your hql queries or change @OneToOne mapping.

Dmytro
  • 83
  • 1
  • 7