I am building a project using the Play framework and I am having trouble getting my head around JPA @OneToOne relationships.
I currently have two classes:
User 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. 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.
However, When I try to set the userId in UserSettings using the code above, I receive the following exception:
A javax.persistence.PersistenceException has been caught, 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. You’re trying to map a String as an entity. Only entities can be a (One|Many)ToOne, as the error is telling you. I.e., instead of
String userId, you should be usingUser user, and instead ofmappedBy="userId",mappedBy="user".