必须手动/分配此/类的ID才能调用save()

时间:2013-07-12 21:20:29

标签: hibernate

我想在我的数据库中保存一个成员。我注释了我的身份,例如:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id",unique=true, nullable = false)
private Integer id;

部分hbm.xml:

<id name="id" type="native">
        <column name="ID" />
        <generator class="increment" />
    </id>

我试图手动添加id ...但是没有工作......我在这里做错了什么?

这是方法:

public Member save() throws ClassNotFoundException, HibernateException, SQLException, InstantiationException, IllegalAccessException {
    Member member=new Member();
    member.setName(this.name);
    member.setSurname(this.surname);
    member.setEmail(this.email);
    member.setUsername(this.username);
    member.setPassword(this.password);

    SessionFactory sf = HibernateUtil.getSessionFactory();
    Session session = sf.openSession();
    session.beginTransaction();

    session.save(member);


    session.getTransaction().commit();

    session.close();

0 个答案:

没有答案