Reputation: 16656
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
Upvotes: 0
Views: 590
Reputation: 3059
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
Upvotes: 2