为什么Hibernate选择两次相同的列?

时间:2010-09-25 16:15:55

标签: java hibernate orm

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

请注意,IDDEPT列会被选中两次。

@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;
}

更新: 有意将@GeneratedValue放在非PK上。但是,现在我按照您的指定进行了更新。

我已经复制粘贴了新的查询:

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

我猜它仍然是一样的。

以下是表格:

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

这里是代码 - 我在这里粘贴:

@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() {
    }

以下是测试用例:

@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 个答案:

答案 0 :(得分:1)

可能是因为这部分:

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

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

这里有问题,GeneratedValue无法应用于非PK。你的意思是:

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

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

如果没有,如果这是故意的,请说明您的目标并显示您的表格。


更新:我无法重现此问题。我复制粘贴你提供的代码,这是我得到的查询:

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

按预期工作。

Google搜索“hibernate duplicate aliases”会显示一些(旧的)问题,所以我不会排除任何内容,但我找不到任何最近存在问题的证据。你能提供一个测试用例(使用嵌入式数据库)吗?