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

  • SEARCH
  • Home
  • 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 7750045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:11:19+00:00 2026-06-01T11:11:19+00:00

I have a table User and another Person , a person is an user

  • 0

I have a table User and another Person, a person is an user, right ?
So I have to persist the user first then get his id so I can set this id in my person entity.

It’s pretty simple, but I don’t know what is wrong with this:

public boolean register(User user, Person person){
    try{
        user.setUserType(new UserType(70, "person"));
        user.setReputation(0);

        em.persist(user);
        em.flush();

        System.out.println("before :" + user.getId());
        Integer id = user.getId();
        System.out.println("after:" + id);

        person.setIdUser(id);
        person.setUser(user);

        em.persist(person);
        //em.flush();
    }catch(Exception e){
        System.out.println("Generic Exception");
        e.printStackTrace();
    }
    return true;
}

The stack error:

FINE: SELECT ID FROM user_type WHERE (ID = ?)
    bind => [1 parameter bound]
FINE: INSERT INTO USER (EMAIL, PASSWORD, REPUTATION, type) VALUES (?, ?, ?, ?)
    bind => [4 parameters bound]
FINE: SELECT LAST_INSERT_ID()
INFO: before :210
INFO: after:210
FINE: INSERT INTO PERSON (BIRTHDATE, GENDER, NAME, SURNAME) VALUES (?, ?, ?, ?)
    bind => [4 parameters bound]
FINE: SELECT 1
WARNING: Local Exception Stack: 
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Field 'id_user' doesn't have a default value
Error Code: 1364
Call: INSERT INTO PERSON (BIRTHDATE, GENDER, NAME, SURNAME) VALUES (?, ?, ?, ?)
    bind => [4 parameters bound]

As you can see, it’s printing the value of the id generated, but I cannot use it, why is that happening ?

EDIT
User Entity:

@Entity
public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @SequenceGenerator(name="USER_ID_GENERATOR", sequenceName="KEY_SEQUENCE")
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="USER_ID_GENERATOR")
    private Integer id;

    private String email;

    private String password;

    private int reputation;

    //bi-directional one-to-one association to Company
    @OneToOne(mappedBy="user")
    private Company company;

    //bi-directional many-to-one association to Location
    @OneToMany(mappedBy="user")
    private List<Location> locations;

    //bi-directional one-to-one association to Person
    @OneToOne(mappedBy="user")
    private Person person;

    //bi-directional many-to-one association to Product
    @OneToMany(mappedBy="user")
    private List<Product> products;

    //bi-directional many-to-one association to UserType
    @ManyToOne
    @JoinColumn(name="type")
    private UserType userType;

    //bi-directional many-to-one association to UserPhone
    @OneToMany(mappedBy="user")
    private List<UserPhone> userPhones;

    //bi-directional many-to-one association to UserPicture
    @OneToMany(mappedBy="user")
    private List<UserPicture> userPictures;

    //bi-directional many-to-one association to UserSocialNetwork
    @OneToMany(mappedBy="user")
    private List<UserSocialNetwork> userSocialNetworks;

    //get's and set's

Person Entity:

@Entity
public class Person implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @SequenceGenerator(name="PERSON_IDUSER_GENERATOR", sequenceName="KEY_SEQUENCE")
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="PERSON_IDUSER_GENERATOR")
    @Column(name="id_user")
    private int idUser;

    @Temporal( TemporalType.DATE)
    private Date birthdate;

    private String gender;

    private String name;

    private String surname;

    //bi-directional one-to-one association to User
    @OneToOne
    @JoinColumn(name="id_user", updatable=false, insertable=false)
    private User user;

    // get's and set's
  • 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-06-01T11:11:21+00:00Added an answer on June 1, 2026 at 11:11 am

    Without seeing relevant parts of your entities, I can’t be sure what is wrong here. Most likely something is up with how User is mapped in Person. With a normal mapping you really shouldn’t need to flush and get id’s and stuff.

    @Entity
    public class Person implements Serializable
    ...
    @JoinColumn(name = "user_id", referencedColumnName = "id")
    @OneToOne
    private User user;
    ...
    ---- Use in for example register(...)
    User user = new User();
    --- set properties
    em.persist(user); --- only needed if you don't annotate cascade= CascadeType.PERSIST on the relation
    Person person = new Person()
    --- set properties
    person.setUser(user);
    em.persist(person);
    --- no need to flush here - they will be persisted at end of persistence context
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one table containing user sessions and another to indicate violations in the
I have a table (user) that contains user information. I have another table (userview)
I have this table: User id INT PK login VARCHAR UNIQUE I want to
I have a table for user and another table for follower . The followers
I have table user which have fields username,password, and type. The type can be
I have a table User which has the fields (id, first_name, middle_name, last_name). I
I have a table with user items. Each user may have several types of
I have a table like so: Table eventlog user | user_group | event_date |
I'm just learning ruby on rails and I have a table of user roles
I have a User table that has all of their avatars saved in an

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.