【发布时间】:2011-07-21 11:10:51
【问题描述】:
我正在使用 Play 框架构建一个项目,但我无法理解 JPA @OneToOne relationships。
我目前有两个课程:
用户对象
@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;
}
用户设置
@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;
}
}
这个想法是我试图将User 中的userId 字段设置为UserSettings 中的外键。我尝试了几种不同的方法来实现这一点,但我的代码总是抛出错误。我收到的最常见错误是:
引用的属性不是 (One|Many)ToOne。
但是,当我尝试使用上面的代码在UserSettings 中设置userId 时,我收到以下异常:
一个 javax.persistence.PersistenceException 被捕获,org.hibernate.PropertyAccessException: could not get a field value by reflection getter of reader.User.id
谁能帮助解释我如何实现我想要的目标?
【问题讨论】:
标签: java hibernate jpa playframework