2015-02-24 68 views
2

我想使用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的版本(行家):

<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; 
     } 
    } 
} 
+1

我看不到您的數據源配置?你能分享spring和hibernate的版本嗎? – erencan 2015-02-24 19:27:12

+0

數據源正常,因爲我正在使用數據庫登錄。 – user2590727 2015-02-24 19:37:02

+0

如果您在登錄時使用hibernate並且沒問題。上面的代碼也沒關係。但是,我無法看到entityManager有一個數據源。 – erencan 2015-02-24 19:39:05

回答

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 
+0

謝謝你的幫助,但這種方式doasn't工作太...當我在persist()方法中調試代碼時,我發現這個:Object result = method.invoke(target,args); method.invoke在表創建期間返回null ... – user2590727 2015-02-24 20:59:41

+0

嘗試將id與序列相關聯:'author_id INT4 DEFAULT nextval('author_id_seq')NOT NULL' – 2015-02-24 22:22:22

0

我已經解決了這個問題。我不得不補充上下文。我將這個標籤添加到根上下文,我雖然這將是好的。