使用注释与属性的多对多关系

时间:2013-08-26 21:27:12

标签: hibernate jpa annotations many-to-many

enter image description here

我想知道如何使用注释@ManyToMany创建表role_permission,同时考虑表role_permission中的属性created_at。

我知道我可以这样做:

public class Role implements Serializable{

    @Id
    @Column(name = "_id")
    private String id;

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

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

    @ManyToMany(cascade = {CascadeType.ALL})
    @JoinTable(name="role_permission", schema=joinColumns={@JoinColumn(name="idRole")}, inverseJoinColumns={@JoinColumn(name="idPermission")})
    private Set<Permission> permissions=new HashSet();

public class Permission implements Serializable{

    @Id
    @Column(name = "_id")
    private String id;

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

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

    @ManyToMany(cascade = {CascadeType.ALL})
    @JoinTable(name="role_permission", schema=joinColumns={@JoinColumn(name="idPermission")}, inverseJoinColumns={@JoinColumn(name="idRole")})
    private Set<Permission> roles=new HashSet();

有了这个,我可以避免为role_permission创建一个新类,但我不知道如何将created_at放在这个注释中。这可能吗?

1 个答案:

答案 0 :(得分:6)

我放弃了,我使用了中介。我刚刚按照本教程进行操作,效果非常好。

http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/