property mapping has wrong number of columns exception - Play-framework
I am a beginner with play framework.Again a question on JPA and mappings in play framework,
I have a student table and a mentor table bound by a one to one relationship.
Student table :
id, name, class, grade
Mentor table:
id, name, department, student_id
In the above, a mentor may or may not have a student bound to him/her. I am making the mentor Model with a one to one mapping,
@OneToOne
@JoinColumn(name="fk_student_id", referencedColumnName="id")
private student Student;
When I try to run this, I get an
A JPA error occurred (Unable to build EntityManagerFactory): property mapping has wrong number of columns: models.Mentor.student type: models.Student.
I am sure I have mapped all the Student fields as below,
Student.java
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@Column(name="name")
private String name;
@Column(name="class")
private String cls;
@Column(name="grade")
private String grade;
What am I missi开发者_JS百科ng here?
Thanks for your time.
Regards, Abi
Are you sure this is working code for Play Framework? There are some differences between Play and standard JPA when creating your model. This fragment:
@OneToOne
@JoinColumn(name="fk_student_id", referencedColumnName="id")
private student Student;
is wrong. Should be something like
@OneToOne
@JoinColumn(name="fk_student_id") //removed the id reference, let JPA manage it
public Student student; //note order of class and var name
Also, you are defining an 'id' field, which is not needed when you extend from Model. Are you extending from Model?
Also you can use GenericModel
if you want some control on your id and sequence strategy
From the play official documentation:
Custom id mapping with GenericModel:
Nothing forces you to base your entities on play.db.jpa.Model. Your JPA entities can also extend the play.db.jpa.GenericModel class. This is required if you do not want to use a Long id as the primary key for your entity.For example, here is a mapping for a very simple User entity. The id is a UUID, the name and mail properties are required, and we use Play Validation to enforce simple business rules.
@Entity
public class User extends GenericModel {
@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
public String id;
@Required
public String name;
@Required
@MaxSize(value=255, message = "email.maxsize")
@play.data.validation.Email
public String mail;
}
精彩评论