我刚刚更新到 Hibernate 4.1.5 和 Spring 3.1.2(从 2.6ish 和 2.5 或其他东西......)。我已经设法将所有内容设置为从现有数据库中读取,并且所有页面都显示正常,但是在提交数据时没有任何内容被持久化到数据库中。我已将日志记录设置为跟踪级别以尝试跟踪此问题,并且事务归结为从我的日志中提取的以下 15 行:
DEBUG - org.hibernate.internal.SessionImpl - SessionImpl - Opened session at timestamp: 13430582784
TRACE - org.hibernate.internal.SessionImpl - SessionImpl - Setting flush mode to: AUTO
TRACE - org.hibernate.internal.SessionImpl - SessionImpl - Setting cache mode to: NORMAL
TRACE - org.hibernate.event.internal.AbstractSaveEventListener - AbstractSaveEventListener - Transient instance of: ic.entities.MyEntity
TRACE - org.hibernate.event.internal.DefaultPersistEventListener - DefaultPersistEventListener - Saving transient instance
TRACE - org.hibernate.event.internal.AbstractSaveEventListener - AbstractSaveEventListener - Saving [ic.entities.MyEntity#<null>]
TRACE - org.hibernate.engine.spi.ActionQueue - ActionQueue - Adding an EntityIdentityInsertAction for [ic.entities.MyEntity] object
TRACE - org.hibernate.engine.spi.ActionQueue - ActionQueue - Adding insert with no non-nullable, transient entities: [EntityIdentityInsertAction[ic.entities.MyEntity#<delayed:0>]]
TRACE - org.hibernate.engine.spi.ActionQueue - ActionQueue - Adding resolved non-early insert action.
TRACE - org.hibernate.action.internal.UnresolvedEntityInsertActions - UnresolvedEntityInsertActions - No unresolved entity inserts that depended on [[ic.entities.MyEntity#<delayed:0>]]
TRACE - org.hibernate.action.internal.UnresolvedEntityInsertActions - UnresolvedEntityInsertActions - No entity insert actions have non-nullable, transient entity dependencies.
TRACE - org.hibernate.internal.SessionImpl - SessionImpl - Closing session
TRACE - org.hibernate.engine.jdbc.internal.LogicalConnectionImpl - LogicalConnectionImpl - Closing logical connection
TRACE - org.hibernate.engine.jdbc.internal.JdbcResourceRegistryImpl - JdbcResourceRegistryImpl - Closing JDBC container [org.hibernate.engine.jdbc.internal.JdbcResourceRegistryImpl@def577d]
TRACE - org.hibernate.engine.jdbc.internal.LogicalConnectionImpl - LogicalConnectionImpl - Logical connection closed
我首先注意到它会创建一条日志消息:正在保存 [ic.entities.MyEntity#< null>]。但是手动单步执行代码表明这是在 id 生成之前发生的,所以我想它应该看起来像这样......我起初以为第 10 和 11 行是错误的,但在单步执行代码之后似乎这些是唯一不会引发异常的有效代码路径,但可以看出会话关闭并且没有保存任何条目......任何帮助将不胜感激!
我使用以下代码:
我的服务.java
package ic.services;
import ic.entities.MyEntity;
import java.util.List;
public abstract class MyService extends BasicService<MyEntity>
{
protected MyService()
{
super(MyEntity.class);
}
public abstract List<MyEntity> getAllByQuery(String query);
}
基本服务.java
package ic.services;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
public abstract class BasicService<T extends Serializable>
{
@PersistenceContext protected EntityManager em;
private final Class<T> entityClass;
protected BasicService(Class<T> entityClass)
{
this.entityClass = entityClass;
}
public T getEntity(Object primaryKey)
{
return em.find(entityClass, primaryKey);
}
public void persist(T entity)
{
em.persist(entity);
}
public void delete(T entity)
{
em.remove(entity);
}
}
我的实体.java
package ic.entities;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table
public class MyEntity implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String notes;
private Date submittedDate;
private boolean enabled;
public Long getId()
{
return id;
}
public void setId(Long id)
{
this.id = id;
}
public Date getSubmittedDate()
{
return submittedDate;
}
public void setSubmittedDate(Date submittedDate)
{
this.submittedDate = submittedDate;
}
public String getNotes()
{
return notes;
}
public void setNotes(String notes)
{
this.notes = notes;
}
public boolean isEnabled()
{
return enabled;
}
public void setEnabled(boolean enabled)
{
this.enabled = enabled;
}
@Override
public boolean equals(Object obj)
{
if(obj instanceof MyEntity)
{
MyEntity rhs = (MyEntity)obj;
return rhs.id == this.id;
}
return false;
}
@Override
public int hashCode()
{
return Long.valueOf(id).hashCode();
}
}
并称之为:
@SpringBean MyService myService; //(Using wicket @Springbean)
...
MyEntity entity = new MyEntity();
entity.setEnabled(true);
entity.setNotes("blah");
entity.setSubmittedDate(new Date());
myService.persist(entity);
我有 web.xml 作为:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>OpenEntityManagerFilter</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenEntityManagerFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
最后在 applicationContext.xml 中:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"></bean>
<tx:annotation-driven/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<context:annotation-config />
<context:component-scan base-package="ic.services" />
</beans>
同样,任何帮助都会很棒。我敢肯定,这对我来说一定是一件非常愚蠢的事情!:)