Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7032927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:59:50+00:00 2026-05-28T00:59:50+00:00

I have two entities person and Student , the student entity extends person and

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:59:51+00:00Added an answer on May 28, 2026 at 12:59 am

    Based on your follow-up comments what you want is to have a primary key identifier for Person, and a unique identifier for Student. 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 for Person:

    @Entity
    @Table(name="person")
    @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
    @DiscriminatorColumn(name="TYPE", discriminatorType=DiscriminatorType.STRING,length=20)
    @DiscriminatorValue("P")
    public class Person {
        @Basic
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name="personId")
        private Long id;
    }
    

    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:

    @Entity
    @DiscriminatorValue("S")
    public class Student extends Person {
       @Basic
       @TableGenerator(name="ID_GEN", table="identifier_table", pkColumnName="seq_name",
            valueColumnName="seq_count", pkColumnValue="student_seq")
       @GeneratedValue(strategy = GenerationType.TABLE, generator = "ID_GEN")
       @Column(name = "studentNumber")
       private Long studentNumber;
    }
    

    Heres the DDL for the table you will need to support sequence number generation for Students, given the example above:

    create table identifier_table (
      id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
      seq_name VARCHAR(255) NOT NULL,
      seq_count INT NOT NULL DEFAULT 0
    );
    

    And you would initially populate it with:

    insert into identifier_table (seq_name, seq_count) VALUES ('student_seq', 0);
    

    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:

    public class Student extends Person {
        @Basic
        @NotNull
        @Column(name = "studentId")
        private String studentIdentifier;
    }
    

    And before saving each record, set the student ID with the UUID class:

    aStudent.setStudentIdentifier(UUID.randomUUID().toString());
    

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 entities (and two tableViews) my first entity name is Person second
I have the following two entities: Person.java: @Entity @Table(name=PERSON) public class Person { @Id
I have a parent entity, Person, and two children entities : Caller and Employee.
I have a first abstract Entity : Person. Two entities inherit from Person :
I have two entities, Parent and Child. The Parent entity has a to-many relationship
A have two entities. For example timing settings and orders. @Entity public class TimingSettings{
I have two entities say Customer and Order which exist on their own and
I have two entities: Patient and Job. Patient has a to-many relationship to Job
I have two entities types: RunContainer parent entity type Run child entity type Run
I have two entities like public class Person { public int PersonId { get;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.