[英]hibernate doesn't add new record to database
I would like to add new record to databse using hibernate and entityManager. 我想使用hibernate和EntityManager将新记录添加到数据库。 When I use entityMenager.persist(myObject) console displays "Hibernate: select nextval ('hibernate_sequence')" but I don't see below insert sql.
当我使用entityMenager.persist(myObject)控制台时显示“休眠:选择nextval('hibernate_sequence')”,但在下面看不到插入sql。
It is my entity class: 这是我的实体类:
@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;
}
}
And it is DAO: 这是DAO:
@Repository
@Transactional
public class AuthorDAO {
@Autowired
private EntityManager entityManager;
public void addNewAuthor(){
Author newAuthor = new Author();
newAuthor.setAuthor("Dan Brown");
entityManager.persist(newAuthor);
}
}
Configuration is below: 配置如下:
<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>
and context: 和上下文:
<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's versions ( maven ): 框架版本(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>
I am using entityMenager to login and is ok. 我正在使用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;
}
}
}
From hibernate documentation you should use GenerationType.SEQUENCE
从休眠文档中,您应该使用
GenerationType.SEQUENCE
Entity 实体
@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 PostgreSQL的
CREATE SEQUENCE author_id_seq INCREMENT 1 START 1
I have resolved this problem. 我已经解决了这个问题。 I had to add to context .
我必须添加上下文。 I added this tag to root-context and I though that will be ok .
我将此标签添加到root-context中,尽管可以。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.