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=?
Обратите внимание, что столбцы ID
и DEPT
выбираются дважды.
@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);
}
}