开发者

Create ORMapping using OneToMany relations for 3 tables

开发者 https://www.devze.com 2023-02-14 09:22 出处:网络
I have 3 tables: Project, User and Role. Now I would like to have a table Project2User2Role with the embed开发者_如何学JAVAded key id_project, id_user,.

I have 3 tables: Project, User and Role. Now I would like to have a table Project2User2Role with the embed开发者_如何学JAVAded key id_project, id_user,.

I tried it using @OneToMany relations in all three entities but I think I cannot build it like that.

And I also tried to build a Project2User2Role entity class by my self but than I have to make an idclass without the @ManyToOne relations.

How could the solution look like?


The reference manual says that you can embed a relationship inside an embedded ID :

While not supported in JPA, Hibernate lets you place your association directly in the embedded id component

You should thus define a Project2User2Role entity with an ID of type Project2User2RoleId:

@Entity
public class Project2User2Role {
    @EmbeddedId
    private Project2User2RoleId id;

    public User getUser() {
        return this.id.getUser();
    }

    public Project getProject() {
        return this.id.getProject();
    }

    // ...
}

The Project2User2RoleId class would look like this :

@Embeddable 
public class Project2User2RoleId {
    @ManyToOne(optional = false)
    @JoinColumn(name = "project_id")
    private Project project;

    @ManyToOne(optional = false)
    @JoinColumn(name = "user_id")
    private User user;

    @ManyToOne(optional = false)
    @JoinColumn(name = "role_id")
    private Role role;

    // constructor
    // getters
    // equals and hashCode
}


For JPA 1 you can't use entities as an Id. From JPA 1 Spec (2.1.14)

The primary key (or field or property of a composite primary key) should be one of the following types: any Java primitive type; any primitive wrapper type; java.lang.String; java.util.Date; java.sql.Date.

If you are using JPA 1 you have two alternatives:

A. You will need to create a Project2User2Role with an autogenerated id field and you must specify it as your id of the entity. Then you can add your relationships to Project, User and Role and specify their corresponding annotation mappings (ManyToOne). Example:

@Entity
public class Project2User2Role {

    private Long id;

    private Project project;

    private User user;

    private Role role;

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQPRUSROLID")
    @SequenceGenerator(name="SEQPRUSROLID", sequenceName="SEQPRUSROLID", allocationSize=1)
    public Long getId() {
        return id;
    }

    @ManyToOne
    public Project getProject() {
        return project;
    }

    @ManyToOne
    public User getUser() {
        return user;
    }

    @ManyToOne
    public Role getRole() {
        return role;
    }
    //the setters
}

B. You can create your entity class and keep using a composite id but as JPA 1 Spec you can't specify entities as an id so you will need to use basic columns for that example:

@Entity
public class Project2User2Role {

    @EmbeddedId
    private Project2User2RoleId project2User2RoleId;

    @ManyToOne
    @JoinColumn(insertable=false, updatable = false)
    private Project project;
    @ManyToOne
    @JoinColumn(insertable=false, updatable = false)
    private User user;
    @ManyToOne
    @JoinColumn(insertable=false, updatable = false)
    private Role role;

    //getters/setters
}

    @Embeddable
    class Project2User2RoleId {
        private Long projectId;
        private Long userId;
        private Long roleId;

    }

For JPA 2:

C. You can specify entities as your id for examples and definition please read 2.4 of JPA 2.0 specification:http://jcp.org/aboutJava/communityprocess/final/jsr317/index.html 2.2.3 of Hibernate Documentation: http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#entity-mapping-identifier

0

精彩评论

暂无评论...
验证码 换一张
取 消