views:

74

answers:

1
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);
    }

}
+1  A: 

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)?

Pascal Thivent
@Pascal: I don't think it it becuase I've kept @GeneratedValue for a non-PK. I've checked for other entities. Hibernate is doing the same for almost *all* my entities.
HanuAthena
@HanuAthena You had `deptNameInDeptEntity` declared as `Id` while the mapped column is not a candidate as PK. This can't work. Please show the *current* code and the *current* table. You're doing something wrong (and I do not exclude a global mistake).
Pascal Thivent
@Pascal: Thank you for the reply :) My Bad, i could have posted the NEW code while I updated. Now I've placed everything. Its still the same.
HanuAthena
@Pascal: Can you pl elaborate a bit more on this : "Can you provide a test case (using an embedded database)?" ?. Duplicate aliasing has anything to do with Hibernate Version? My env is : Spring 3.0.3 and Hibernate 3.5.5-Final. (just an fyi)
HanuAthena
@HanuAthena I mean providing a minimal project with an entity, a test, Hibernate configured to use a database like Derby, H2, HSQLDB and to log SQL. Something that would allow to reproduce. Did you actually try with another database? Honestly, I think a bug is unlikely.
Pascal Thivent