簡體   English   中英

休眠帶有注釋的一對多映射-在列表中獲取相同的值

[英]hibernate one to many mapping with annotations - getting same values in the list

我將用戶表與應用程序訪問鏈接在一起。 在這里,一個用戶可以訪問許多應用程序。 我已經使用下面的代碼成功完成了映射。

用戶實體對象:

@Entity
@Table(name = "USER_TBL", uniqueConstraints = { @UniqueConstraint(columnNames = "USER_NAME") })
public class User implements Serializable {

.....
        @Id
    @GeneratedValue
    @Column(name = "USER_ID", unique = true, nullable = false)
    private Integer userId;


    @OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
    private List<UserAppAssociation> userAssociatedApplications = new ArrayList<UserAppAssociation>();

    Getter and setter for userAssociatedApplications

}

應用程序訪問對象:

@Entity
@Table(name="APPLICATION_ASSOC_TBL")
public class UserAppAssociation implements Serializable{

    @Id
    @Column(name="user_id", unique=true, nullable=false)
    private Integer userId;

    @Column(name = "application_id")
    private Integer appId;

    @Column(name = "user_type_id")
    private Integer userTypeId;
    ...
    @ManyToOne
    @JoinColumn(name="USER_ID",insertable=false,updatable=false)
    private User user;

    ..
    getters and setters

}

問題:

I am getting the same values in the Application List ('userAssociatedApplications'). Though i have different values in the application access table, I get the same values in the list. The first row value is repeated in the list.

D B:

I have 'User' table and the mapping is with application access 

用戶表:USER_TBL列user_id名稱電話

應用程序訪問表:APPLICATION_ASSOC_TBL

列User_id,application_id和User_type

注意-此表中沒有主鍵

樣本數據:User_id application_id User_type 1 1 1 1 2 1 1 3 1

問題:我在列表中三次得到第一個值1,1,1。 預期:清單應包含3個不同的值

請幫助。 我不確定在注釋映射中是否缺少任何內容。

看起來像是有問題

@Id
@Column(name="user_id", unique=true, nullable=false)
private Integer userId;

@ManyToOne
@JoinColumn(name="USER_ID",insertable=false,updatable=false)
private User user;

嘗試使用此映射。 請將此作為名稱指南,不要使用不必要的注釋

@Entity
@Table(name = "xxx_users", uniqueConstraints = { @UniqueConstraint(columnNames = "f_name") })
public class User {

    @Id
    @GeneratedValue
    @Column(name = "f_id")
    private Integer id;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
    private List<UserAppAssociation> applications = new ArrayList<UserAppAssociation>();

}


@Entity
@Table(name="xxx_user_applications")
public class UserAppAssociation {

    @Id
    @GeneratedValue
    @Column(name = "f_id")
    private Integer id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="fk_user")
    private User user;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM