hibernate没有向数据库添加新记录

时间:2015-02-24 19:24:05

标签: java spring hibernate

我想使用hibernate和entityManager向数据库添加新记录。当我使用entityMenager.persist(myObject)控制台时显示“Hibernate:select nextval('hibernate_sequence')”但我在下面看不到插入sql。

这是我的实体类:

@Entity
@Table(name = "author")
public class Author implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "author_id")
    private Integer author_id;

    @Column(name = "author")
    private String author;  

    public Integer getAuthor_id() {
        return author_id;
    }

    public void setAuthor_id(Integer author_id) {
        this.author_id = author_id;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }
}

它是DAO:

@Repository
@Transactional
public class AuthorDAO {

    @Autowired
    private EntityManager entityManager;

    public void addNewAuthor(){
        Author newAuthor = new Author();
        newAuthor.setAuthor("Dan Brown");
        entityManager.persist(newAuthor);
    }
}

配置如下:

<persistence-unit name="engineerJPA" transaction-type="JPA">
  <class>com.engineering.pawel.entity.User</class>
  <class>com.engineering.pawel.entity.UserRole</class>
  <class>com.engineering.pawel.entity.Author</class>

  <jta-data-source>java:jboss/datasources/postgreSQL</jta-data-source>

  <properties>
   <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
   <property name="hibernate.max_fetch_depth" value="3" />
   <property name="hibernate.hbm2ddl.auto" value="update" />
   <property name="hibernate.show_sql" value="true" />
   <property name="hibernate.jdbc.batch_size" value="100" />
   <property name="hibernate.id.new_generator_mappings" value="true" />
   <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup" />
   <property name="hibernate.transaction.auto_close_session" value="true" />
   <property name="javax.persistence.transactionType " value="jta" />
   <property name="hibernate.current_session_context_class" value="jta" />
   <property name="hibernate.connection.release_mode" value="auto" />
  </properties>
 </persistence-unit>
</persistence>

和上下文:

<context:annotation-config />
    <context:component-scan base-package="com.engineering.pawel" />

    <!-- Database configuration -->
    <tx:annotation-driven />

    <jee:jndi-lookup id="entityManagerFactory" jndi-name="java:comp/env/persistence/emf"
        expected-type="javax.persistence.EntityManagerFactory" />

    <beans:bean id="transactionManager"
        class="org.springframework.transaction.jta.JtaTransactionManager">
        <beans:property name="transactionManagerName" value="java:/TransactionManager" />
    </beans:bean>

    <beans:bean id="persistenceExceptionTranslationPostProcessor"
        class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

    <beans:bean id="entityManager"
        class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
        <beans:property name="entityManagerFactory" ref="entityManagerFactory" />
    </beans:bean>

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="get*" read-only="true" />
            <tx:method name="find*" read-only="true" />
            <tx:method name="*" />
        </tx:attributes>
    </tx:advice>

Framework的版本(maven):

<spring.version>4.0.0.RELEASE</spring.version>
        <spring.security.version>3.2.5.RELEASE</spring.security.version>
        <hibernate-version>4.0.1.Final</hibernate-version>

我正在使用entityMenager登录并且没问题。

@Repository
public class UserDAO{

    @Autowired
    private EntityManager entityManager;

    @SuppressWarnings("unchecked")
    public List<User> getAllUsers(){
        Query query = entityManager.createQuery("from User");
        List<User> listUsers = query.getResultList();
        return listUsers;
    }

    @SuppressWarnings("unchecked")
    public User findByUserName(String userName){

        List<User> users = new ArrayList<User>();

        Query query = entityManager.createQuery("from User where nick = :nick").setParameter("nick", userName);
        users = query.getResultList();

        if(users.size() > 0){
            return users.get(0);
        }else{
            return null;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

从休眠documentation开始,您应该使用GenerationType.SEQUENCE

实体

@Id
@SequenceGenerator(name = "author_id_seq", sequenceName = "author_id_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "author_id_seq")
@Column(name = "author_id")

的PostgreSQL

CREATE SEQUENCE author_id_seq INCREMENT 1 START 1

答案 1 :(得分:0)

我已经解决了这个问题。我不得不添加上下文。我将此标记添加到root-context中,虽然这样就可以了。