【问题标题】:Cannot set lastest id inserted in Entity无法设置实体中插入的最新 ID
【发布时间】:2012-04-15 12:22:59
【问题描述】:

我有一张桌子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

【问题讨论】:

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


【解决方案1】:

没有看到您实体的相关部分,我无法确定这里出了什么问题。很可能与用户在个人中的映射方式有关。使用法线贴图,您真的不需要刷新和获取 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

【讨论】:

  • 原始问题编辑后:据我了解,“updatable=false, insertable=false”仅在实体中使用/映射两次基础数据时使用(复合主键和其他一些场景) 你不应该在这里使用它。但我没有可更新/可插入的经验。
猜你喜欢
  • 2021-08-01
  • 1970-01-01
  • 2018-06-22
  • 1970-01-01
  • 2020-11-14
  • 2016-07-05
  • 2015-11-27
  • 1970-01-01
  • 2019-03-01
相关资源
最近更新 更多