Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is Hibernate selecting same columns twice?

Hibernate: 
    /* load entities.Department */ select
        department0_.name as name4_0_,
        department0_.id as id4_0_ 
    from
        J_DEPT department0_ 
    where
        department0_.name=?
Hibernate: 
    /* load one-to-many entities.Department.employees */ select
        employees0_.dept as dept4_1_,
        employees0_.id as id1_,
        employees0_.id as id5_0_,
        employees0_.dept as dept5_0_,
        employees0_.name as name5_0_ 
    from
        J_EMP employees0_ 
    where
        employees0_.dept=?

Note that ID and DEPT columns are selected twice.

@Entity
@Table(name = "J_EMP")
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "mySeq")
    @SequenceGenerator(name = "mySeq", sequenceName = "CNTRY_SEQ")
    private Long id;
    private String name;

    @ManyToOne
    @JoinColumn(name = "dept")
    private Department deptNameInEmp;
}


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

    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "mySeq")
    @SequenceGenerator(name = "mySeq", sequenceName = "CNTRY_SEQ")
    private Long id;

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

    @OneToMany(mappedBy = "deptNameInEmp")
    Set<Employee> employees;
}

UPDATED: It was done intentionally to put @GeneratedValue on non-PK. However, now I've updated as you specified.

I've copy-pasted the new queries:

Hibernate: 
    /* load entities.Department */ select
        department0_.id as id4_0_,
        department0_.name as name4_0_ 
    from
        J_DEPT department0_ 
    where
        department0_.id=?
Hibernate: 
    /* load one-to-many entities.Department.employees */ select
        employees0_.dept as dept4_1_,
        employees0_.id as id1_,
        employees0_.id as id5_0_,
        employees0_.dept as dept5_0_,
        employees0_.name as name5_0_ 
    from
        J_EMP employees0_ 
    where
        employees0_.dept=?

I guess its still the same.

And here are the tables:

CREATE TABLE "XYZ"."J_DEPT" 
   (    "ID" NUMBER(*,0) NOT NULL ENABLE, 
    "NAME" VARCHAR2(255 BYTE) NOT NULL ENABLE, 
     CONSTRAINT "J_DEPT_PK" PRIMARY KEY ("ID")
)

CREATE TABLE "XYZ"."J_EMP" 
   (    "ID" NUMBER NOT NULL ENABLE, 
    "NAME" VARCHAR2(255 BYTE), 
    "DEPT" NUMBER NOT NULL ENABLE, 
     CONSTRAINT "J_EMP_PK" PRIMARY KEY ("ID"))

here is the code -i'm pasting here as it is :

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

    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "mySeq")
    @SequenceGenerator(name = "mySeq", sequenceName = "CNTRY_SEQ")
    @Id
    private Long id;

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

    @OneToMany(mappedBy = "deptNameInEmp")
    Set<Employee> employees;

    public Set<Employee> getEmployees() {
        return employees;
    }
}



@Entity
@Table(name = "J_EMP")
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "mySeq")
    @SequenceGenerator(name = "mySeq", sequenceName = "CNTRY_SEQ")
    private Long id;
    private String name;

    @ManyToOne
    @JoinColumn(name = "dept")
    private Department deptNameInEmp;

    public Employee() {
    }

And here is the test case:

@RunWith(SpringJUnit4ClassRunner.class)
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)
@ContextConfiguration(locations = { "classpath:test-applicationContext.xml" })
@Transactional
public class EmpTest {

    @Autowired
    private SessionFactory sessionFactory;

    @Test
    public void testDept() {
        final Department find = (Department) sessionFactory.getCurrentSession()
                .get(Department.class, Long.parseLong("1"));
        System.out.println("find res = " + find);
    }

}
like image 824
jai Avatar asked Nov 14 '22 07:11

jai


1 Answers

Probably because of this part:

@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "mySeq")
@SequenceGenerator(name = "mySeq", sequenceName = "CNTRY_SEQ")
private Long id;

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

There is something wrong here, the GeneratedValue cannot be applied to a non PK. Did you mean:

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "mySeq")
@SequenceGenerator(name = "mySeq", sequenceName = "CNTRY_SEQ")
private Long id;

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

If not, and if this was intentional, please explain your goal and show your table(s).


Update: I can't reproduce the problem. I copy pasted the code you provided and here is the query I get:

select
  employee37x0_.id as id135_,
  employee37x0_.dept as dept135_,
  employee37x0_.name as name135_ 
 from
  J_EMP employee37x0_ 
 where
  employee37x0_.id=?

Works as expected.

A Google search on "hibernate duplicate aliases" reveals some (old) issues so I don't exclude anything but I couldn't find any proof of recent existing problems. Can you provide a test case (using an embedded database)?

like image 155
Pascal Thivent Avatar answered Dec 21 '22 01:12

Pascal Thivent