У меня есть таблица User
и еще один Person
, person
- это user
, верно?Поэтому я должен сначала сохранить пользователя, а затем получить его идентификатор, чтобы я мог установить этот идентификатор в своей сущности.
Это довольно просто, но я не знаю, что с этим не так:
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;
}
Ошибка стека:
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]
Как видите, он печатает значение сгенерированного идентификатора, но я не могу его использовать, почему это происходит?
РЕДАКТИРОВАТЬ Пользовательский объект:
@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
Персональный объект:
@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