JPA中有多个可写映射? [英] Multiple writable mappings in JPA?

查看:158
本文介绍了JPA中有多个可写映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对我的系统中的某位用户进行审核,此审核是由其他用户完成的。所以这里是我的用户表:





这是我的user_review表:



> @PrimaryJoinColumn 是 @JoinColumn 它改变了它,因为

  @Id 
@GeneratedValue(strategy = GenerationType.IDENTITY)
私人整数ID;

@Temporal(TemporalType.DATE)
私人日期日期;

私人字符串评论;

//与用户
的双向多对一关联// @PrimaryKeyJoinColumn(name =id_user_reviewer)
@ManyToOne
@JoinColumn(name =id_user_reviewer)
私人用户审阅者;

//与用户
的双向多对一关联// @JoinColumn(name =id_user)
@ManyToOne
@PrimaryKeyJoinColumn(name =id_user)
私人用户用户;

//与UserInfo的双向多对一关联
@ManyToOne
@JoinColumn(name =id_user)
private UserInfo userInfo;

一直给我这个错误:

<$



无法在表user_review上解析加入列user_id我真的不该做什么,因为我已经尝试了很多东西,不同的协会,但似乎没有任何工作。任何想法?

嗯 - 您的总结:

JPA中的多个可写映射?



与您的问题似乎不符:

为什么我得到这个错误:加入列user_id无法解析表user_review?



所以我猜你会遇到多个可写入.. 。在你的用户字段中指定 @JoinColumn 时出现问题;当您在用户上指定 @PrimaryKeyJoinColumn 时,您会遇到...无法解析...问题>字段。

@PrimaryKeyJoinColumn 不能用于 @ManyToOne 映射。 (当实体共享主键时,它可以在 @OneToOne 映射中使用)。结果,Eclipse正在计算JPA规范定义的默认连接列名称( user_id),并且由于该列不在您的 user_review 表中,您会收到一条错误消息,指示该字段无法在数据库表中解析。



您应该取消注释 @JoinColumn c $ c> user 字段。这导致了两个 @OneToMany 映射,这些映射将确定要写入数据库的 id_user 列的外键 - 它将采用由用户字段或<$ c $>引用的用户的主键的值c> UserInfo 由 userInfo 字段引用。这是不允许的。您可能需要重新修改数据库关系或对象模型或将其中一个映射标记为只读( @JoinColumn(name =id_user,insertable = false,updatable = false))。


I would like to make a review about some user at my system, this review is made by others users. So here it's my user's table:

And this is my user_review table:

The EclipseLink is generating like this (actually it insted of @PrimaryJoinColumn was @JoinColumn it change it because of this post ):

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;

    @Temporal(TemporalType.DATE)
    private Date date;

    private String review;

    //bi-directional many-to-one association to User
//  @PrimaryKeyJoinColumn(name="id_user_reviewer")
    @ManyToOne 
    @JoinColumn(name="id_user_reviewer")
    private User reviewer;

    //bi-directional many-to-one association to User
//  @JoinColumn(name="id_user")
    @ManyToOne 
    @PrimaryKeyJoinColumn(name="id_user")
    private User user;

    //bi-directional many-to-one association to UserInfo
    @ManyToOne
    @JoinColumn(name="id_user")
    private UserInfo userInfo; 

And keeps giving me this error:

Join column "user_id" cannot be resolved on table "user_review"

I really don't what to do 'cause I already try many things, different associations but nothing seems to be working. Any ideas ?

解决方案

Hmmm - your summary:

Multiple writable mappings in JPA?

does not match what seems to be your question:

Why do I get this error: Join column "user_id" cannot be resolved on table "user_review"?

So I'm guessing you experience the "multiple writable..." problem when you specify a @JoinColumn on your user field; and you get the "...cannot be resolved..." problem when you specify a @PrimaryKeyJoinColumn on your user field.

@PrimaryKeyJoinColumn cannot be used on a @ManyToOne mapping. (It can be used on a @OneToOne mapping when entities share a primary key.) As a result, Eclipse is calculating the default join column name as defined by the JPA spec (which is "user_id"), and since that column is not on your user_review table, you get an error message indicating the field cannot be resolved on the database table.

You should un-comment the @JoinColumn on your user field. That results in two @OneToMany mappings that will determine the foreign key to be written to the id_user column on the database - it will take the value of the primary key of either the User referenced by the user field or the UserInfo referenced by the userInfo field. This is not allowed. Either you need to rework your database relations or your object model or mark one of the mappings read-only (@JoinColumn(name="id_user", insertable = false, updatable = false)).

这篇关于JPA中有多个可写映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆