gpt4 book ai didi

java - hibernate 不向数据库添加新记录

转载 作者:行者123 更新时间:2023-12-05 07:56:29 24 4
gpt4 key购买 nike

我想使用 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>

框架的版本(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;
}
}
}

最佳答案

来自 hibernate 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

关于java - hibernate 不向数据库添加新记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28704446/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com