无法设置在实体中插入的最新ID

时间:2012-04-01 17:00:56

标签: database jpa ejb ejb-3.0 jpa-2.0

我有一个表User和另一个Personpersonuser,对吗? 所以我必须首先坚持用户然后获取他的id,以便我可以在我的person实体中设置这个id。

这很简单,但我不知道这有什么问题:

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]

正如你所看到的,它正在打印生成的id的值,但我无法使用它,为什么会发生这种情况?

修改 用户实体:

@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

1 个答案:

答案 0 :(得分:2)

如果没有看到您实体的相关部分,我无法确定这里有什么问题。最有可能的是用户在Person中的映射方式。使用法线贴图,你真的不需要刷新并获取id和东西。

@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