向数据库添加多个实例

时间:2015-05-05 09:20:48

标签: java hibernate

我有一个简单的JSF + Hibernate + Spring + Mysql java应用程序,我在Tomcat7.On JSF视图页面上运行它,它只有一个添加按钮,可以通过点击它将这些实例添加到数据库。它运行时完美添加第一个实例。但是,当我尝试在同一个会话中再添加一个实例时,程序失败并出现以下错误:

  An Error Occurred:

org.hibernate.PersistentObjectException: detached entity passed to persist: com.springhibernatejsf.model.Person

在Google上搜索此错误后,我发现可能是由于模态类中@GeneratedValue(strategy=GenerationType.IDENTITY)而不是@GeneratedValue(strategy=GenerationType.AUTO),或者在DAO实现类中使用session.persist()而不是session.merge()但是,他们俩都没有为我工作。

Person.java:

package com.springhibernatejsf.model;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * Entity bean with JPA annotations
 * 
 *
 */
@Entity
@Table(name="Person")
@ManagedBean(name="person")
@SessionScoped
public class Person implements Serializable{
    @Id
    @Column(name="id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    private String name;

    private String country;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }
}

PersonDAOImpl.java:

package com.springhibernatejsf.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.springhibernatejsf.model.Person;

@Repository
public class PersonDAOImpl implements PersonDAO {

    private SessionFactory sessionFactory;

    private static final Logger logger = LoggerFactory.getLogger(PersonDAOImpl.class);

    public void setSessionFactory(SessionFactory sf){
        this.sessionFactory = sf;
    }
    @Override
    public void addPerson(Person p){
        Session session = this.sessionFactory.getCurrentSession();
        session.persist(p);
        session.flush();
        logger.info("Addition is successful!");
    }
    @SuppressWarnings("unchecked")
    @Override
    public List<Person> listPersons() {
        Session session = this.sessionFactory.getCurrentSession();
        List<Person> personsList = session.createQuery("from Person").list();
        for(Person p : personsList){
        }
        return personsList;
    }
}

为什么会这样?

1 个答案:

答案 0 :(得分:0)

PersonDaoImpl

addPerson()中尝试使用以下代码
public void addPerson(Person p){
        Session session = this.sessionFactory.getCurrentSession();
        Transaction transaction=session.beginTransaction();
        session.persist(p);
        session.flush();
        logger.info("Addition is successful!");
        transaction.commit();
    }

必须在persist()边界内使用transaction