I have two entities person and Student, the student entity extends person and has its own identifier studentNumber. When creating a new student, i want to auto-generate both identification numbers.
The following snippet fails with: person_id being NULL
@RooEntity(identifierColumn = "personID", inheritanceType = "SINGLE_TABLE")
@DiscriminatorColumn(name = "TYPE", discriminatorType = DiscriminatorType.STRING, length = 20)
@DiscriminatorValue("P")
public class Person {
}
The student Entity
@RooEntity
@DiscriminatorValue("S")
public class Student extends Person {
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "studentNumber")
private long studentNumber;
}
Here is where i create a new student and the student number is generated but personid is not?
private Student student;
/**
* @method Create a new student using our persistence model---Not Null variables
*/
public void CreateNewStudent(String firstName, String lastName, String telephone, Date birthday, GenderType gender){
student = new Student();
//set our variables and persist
student.setFirstName(firstName);
student.setLastName(lastName);
student.setTelephone(telephone);
student.setBirthDay(birthday);
student.setGender(gender);
student.persist();
}
}
is there any way i can create both studentNumber and personID when a student is created?
Based on your follow-up comments what you want is to have a primary key identifier for
Person, and a unique identifier forStudent. Note that with your current setup, only one of the keys can be auto generated (many DBMSes only allow one column per table to be auto incremented). So I would recommend that you first define your primary key column forPerson:Note that in your DB you will want to have the personId column as the auto-increment. Then define a separate generated value annotation for your student numbers. The example I show below uses a table generator but you can do whatever you want:
Heres the DDL for the table you will need to support sequence number generation for Students, given the example above:
And you would initially populate it with:
Of course, the simpler solution would be to declare your student identifier’s as String, then you could just do the following, much easier setup:
And before saving each record, set the student ID with the UUID class:
Hope this helps.