以下示例工作正常,我希望您检查这是否是一个好方法,因为我之前没有这样的要求,我很好奇应该如何完成。
我有三个表:EMPLOYEE,MEETING,EMPLOYEE_MEETING以及EMPLOYEE和MEETING之间的MANY-TO-MANY关系只有两个映射。看来我能够插入,加载和更新所有内容。我想要实现的不是为关系创建额外的映射,DAO和单独的模型。
CREATE TABLE "employee_meeting" (
"employee_id" bigint(20) NOT NULL,
"meeting_id" bigint(20) NOT NULL,
"opinion" varchar(50) DEFAULT NULL,
PRIMARY KEY ("employee_id","meeting_id"),
KEY "FK_MEETING" ("meeting_id"),
CONSTRAINT "FK_EMPLOYEE" FOREIGN KEY ("employee_id") REFERENCES "employee" ("employee_id"),
CONSTRAINT "FK_MEETING" FOREIGN KEY ("meeting_id") REFERENCES "meeting" ("meeting_id")
) ENGINE=InnoDB DEFAULT CHARSET=latin1$$
<hibernate-mapping package="net.viralpatel.hibernate">
<class name="Employee" table="EMPLOYEE">
<id name="employeeId" column="EMPLOYEE_ID">
<generator class="native" />
</id>
<property name="firstname" />
<property name="lastname" column="lastname" />
<set name="meetings" table="EMPLOYEE_MEETING" inverse="false" lazy="false" fetch="select" cascade="all" >
<key column="EMPLOYEE_ID" />
<composite-element class="EmployeeMeeting">
<parent name="employee" />
<many-to-one name="meeting" column="MEETING_ID" not-null="true" cascade="all"
class="Meeting" />
<property name="opinion" column="OPINION" />
</composite-element>
</set>
</class>
<class name="Meeting" table="MEETING">
<id name="meetingId" type="java.lang.Long"
column="MEETING_ID">
<generator class="native" />
</id>
<property name="subject" column="SUBJECT" />
<property name="meetingDate" type="date" column="MEETING_DATE" />
</class>
public static void main(String[] args) {
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
Meeting meeting1 = new Meeting("Quaterly Sales meeting");
Meeting meeting2 = new Meeting("Weekly Status meeting");
Employee employee1 = new Employee("Sergey", "Brin");
Employee employee2 = new Employee("Larry", "Page");
session.save(meeting1);
EmployeeMeeting em = new EmployeeMeeting(employee1, meeting1, "BAD");
employee1.getMeetings().add(em);
session.save(employee1);
session.save(employee2);
session.getTransaction().commit();
session.evict(employee1);
session.beginTransaction();
Employee e1 = (Employee) session.load(Employee.class, employee1.getEmployeeId());
EmployeeMeeting em2 = e1.getMeetings().iterator().next();
System.out.println("Test1:" + em2.getOpinion());
em2.setOpinion("BETTER");
session.save(e1);
session.getTransaction().commit();
session.evict(e1);
session.beginTransaction();
Employee e2 = (Employee) session.load(Employee.class, employee1.getEmployeeId());
System.out.println("Test2:" + e2.getMeetings().iterator().next().getOpinion());
System.out.println("Test2:" + e2.getMeetings().iterator().next().getMeeting().getMeetingId());
System.out.println("Test2:" + e2.getMeetings().iterator().next().getEmployee().getEmployeeId());
[/code]
答案 0 :(得分:0)
以这种方式减少Manty-to-Many集合,没有双向,等于OneToMany集合。
我建议尊重多对多集合,在Meeting
类下创建Employee
数组并定义双向映射。
使用什么版本的hibernate?
答案 1 :(得分:0)
从会议课程中我不需要接待员工..我只需要从员工级别访问会议 - 让员工需要插入,更新,删除会议。
我做了一些测试,似乎工作正常。一个奇怪的事情是当我运行这段代码时 - 我只是从数据库加载数据:
Employee e2 = (Employee) session.load(Employee.class, new Long(18));
Employee e = (Employee) session.load(Employee.class, new Long(17));
Iterator i = e.getMeetings().iterator();
while (i.hasNext()){
System.out.println(((EmployeeMeeting) i.next()).getMeeting().getMeetingId());
}
System.out.println(e2.getMeetings().iterator().next().getMeeting().getMeetingId());
session.getTransaction().commit();
session.close();
为什么HIbernate在我只从数据库加载数据时删除和插入会议
Hibernate: select employee0_.EMPLOYEE_ID as EMPLOYEE1_0_0_, employee0_.firstname as firstname0_0_, employee0_.lastname as lastname0_0_ from EMPLOYEE employee0_ where employee0_.EMPLOYEE_ID=?
Hibernate: select meetings0_.EMPLOYEE_ID as EMPLOYEE1_0_, meetings0_.MEETING_ID as MEETING2_0_, meetings0_.OPINION as OPINION0_ from EMPLOYEE_MEETING meetings0_ where meetings0_.EMPLOYEE_ID=?
Hibernate: select employee0_.EMPLOYEE_ID as EMPLOYEE1_0_0_, employee0_.firstname as firstname0_0_, employee0_.lastname as lastname0_0_ from EMPLOYEE employee0_ where employee0_.EMPLOYEE_ID=?
Hibernate: select meetings0_.EMPLOYEE_ID as EMPLOYEE1_0_, meetings0_.MEETING_ID as MEETING2_0_, meetings0_.OPINION as OPINION0_ from EMPLOYEE_MEETING meetings0_ where meetings0_.EMPLOYEE_ID=?
12
11
7
13
7
Hibernate: delete from EMPLOYEE_MEETING where EMPLOYEE_ID=? and MEETING_ID=?
Hibernate: insert into EMPLOYEE_MEETING (EMPLOYEE_ID, MEETING_ID, OPINION) values (?, ?, ?)
Hibernate: delete from EMPLOYEE_MEETING where EMPLOYEE_ID=? and MEETING_ID=?
Hibernate: delete from EMPLOYEE_MEETING where EMPLOYEE_ID=? and MEETING_ID=?
Hibernate: delete from EMPLOYEE_MEETING where EMPLOYEE_ID=? and MEETING_ID=?
Hibernate: delete from EMPLOYEE_MEETING where EMPLOYEE_ID=? and MEETING_ID=?
Hibernate: insert into EMPLOYEE_MEETING (EMPLOYEE_ID, MEETING_ID, OPINION) values (?, ?, ?)
Hibernate: insert into EMPLOYEE_MEETING (EMPLOYEE_ID, MEETING_ID, OPINION) values (?, ?, ?)
Hibernate: insert into EMPLOYEE_MEETING (EMPLOYEE_ID, MEETING_ID, OPINION) values (?, ?, ?)
Hibernate: insert into EMPLOYEE_MEETING (EMPLOYEE_ID, MEETING_ID, OPINION) values (?, ?, ?)