0

我的数据库中有 3 个表。

我在这里开发的架构。

一名员工可以参加多个会议,一名员工可以参加多个会议。

http://sqlfiddle.com/#!4/653a40

我在我的应用程序中使用休眠。

这是我的pojos。

@Entity
@Table(name="emp")
public class Employee
{
@Id
    @Column(name="user_id")
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

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

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

    @ManyToMany(cascade = {CascadeType.ALL})
    @JoinTable(name="emp_meeting", 
            joinColumns={@JoinColumn(name="user_id")}, 
            inverseJoinColumns={@JoinColumn(name="meetingId")})
private Set<MEETING> meetings= new HashSet<MEETING>();


   // getter and setter
}



@Entity
@Table(name="MEETING")
public class MEETING{


    @Id
    @Column(name="meetingId")
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int mid;

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

    @ManyToMany(mappedBy="meetings")
    private Set<Employee> emps= new HashSet<Employee>();

    //gettter and setter
}

这个对吗?因为我不知道如何插入第三张表?

4

2 回答 2

0

您不需要在第三个表中显式插入。当您使用 Cascade.ALL 级联级别时,任何保存/持续调用具有填充会议集的员工都会导致映射表中的自动保存。

于 2013-03-14T11:24:22.817 回答
0

As you association table has no more information than the two foreign key of your main tables, the insertion in this table is managed by hibernate when you associate your objects Employee and MEETING. So to me your mapping is correct, if you encounter problems, don't forget to associate in both direction (that is, in java, if you are adding an employee to a meeting, you should add the meeting to this employee and vice-versa)

(On another topic : your class name should respect java convention naming and be Meeting instead of MEETING).

于 2013-03-14T07:44:13.910 回答