简体   繁体   English

JPA OneToOne 关系

[英]JPA OneToOne relationship

I am building a project using the Play framework and I am having trouble getting my head around JPA @OneToOne relationships .我正在使用 Play 框架构建一个项目,但我无法理解 JPA @OneToOne relationships

I currently have two classes:我目前有两个课程:


User Object用户 Object

@Entity
@Table( name="users" )
public class Users extends Model {

    @OneToOne( mappedBy="userId", fetch=FetchType.LAZY, cascade = CascadeType.ALL )
    @ForeignKey( name="userId", inverseName="userId" )
    UserSettings userSettings;
    public userId;
    public userName;
}


UserSettings用户设置

@Entity
@Table( name="user_settings" )
public class UserSettings extends Model {

    @OneToOne( cascade = CascadeType.ALL,targetEntity=User.class )
    public String userId;
    public String xml;

    public UserSettings( String userId ){
        this.userId = userId;
    }
}


The idea is that I am trying to set the userId field within User as a foreign key within UserSettings .这个想法是我试图将User中的userId字段设置为UserSettings中的外键。 I have tried a few different ways to achieve this and my code always throws an error.我尝试了几种不同的方法来实现这一点,但我的代码总是抛出错误。 The most common error I recveive is: Referenced property not a (One|Many)ToOne .我收到的最常见错误是:引用的属性不是 (One|Many)ToOne

However, When I try to set the userId in UserSettings using the code above, I receive the following exception:但是,当我尝试使用上面的代码在UserSettings中设置userId时,我收到以下异常:

A javax.persistence.PersistenceException has been caught, org.hibernate.PropertyAccessException: could not get a field value by reflection getter of reader.User.id一个 javax.persistence.PersistenceException 已被捕获,org.hibernate.PropertyAccessException: could not get a field value by reflection getter of reader.User.id

Can anybody help explain how I can achieve my desired goal?任何人都可以帮助解释我如何实现我想要的目标吗?

Read section 5.2 of the hibernate reference about the difference between entities and values.阅读 hibernate 参考资料的第 5.2 节,了解实体和值之间的差异。 You're trying to map a String as an entity.您正在尝试将 map 字符串作为实体。 Only entities can be a (One|Many)ToOne, as the error is telling you.正如错误告诉您的那样,只有实体可以是 (One|Many)ToOne。 Ie, instead of String userId , you should be using User user , and instead of mappedBy="userId" , mappedBy="user" .即,您应该使用User user而不是String userId ,而不是mappedBy="userId"mappedBy="user"

If you extend Model, Play will generate an primary key "id" by default for each entity.如果你扩展 Model,Play 默认会为每个实体生成一个主键“id”。 If this is not what you want you should extend Generic model instead.如果这不是您想要的,您应该改为扩展 Generic model。

The simplest way would be to have user as a property of user settings:最简单的方法是将 user 作为用户设置的属性:


@Entity
@Table( name="user_settings" )
public class UserSettings extends Model{
    @OneToOne
    public Users user;
...

@Entity
@Table( name="users" )
public class Users extends Model {
    @OneToOne(cascade = CascadeType.ALL)
    public UserSettings settings;

Maintaining User and UserSettings in each entity with OneToOne allows you to have bi-directional searches.使用 OneToOne 在每个实体中维护 User 和 UserSettings 允许您进行双向搜索。
If you want to use your own key change from Model to GenericModel and defined your foreign key on the user object.如果您想使用您自己的密钥从 Model 更改为 GenericModel 并在用户 object 上定义您的外键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM