我无法使用spring和hibernate来持久化数据。我看了看不同的帖子,尝试了很多事情。但就是不起作用。我将首先发布我的配置,然后我尝试的步骤。会很感激你的帮助。
spring-jpa.xml
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="defaultLobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler" />
<context:component-scan base-package="com.gamelist.dao.classes" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" p:entityManagerFactory-ref="entityManagerFactory"/>
<tx:annotation-driven transaction-manager="transactionManager"/>GenericDAO.java
public class GenericDaoJPA<T extends IDomainObject> implements IGenericDao<T> {
protected EntityManager entityManager;
@PersistenceContext
public void setEntityManager(EntityManager entityManager){
this.entityManager = entityManager;
}
public void save(T object) throws DataAccessException{
entityManager.persist(object);
}
.
.
.
.
}User.java (域)
@Entity
@Table(name = "user")
public class User implements Serializable, IDomainObject{
private long id;
private String firstName;
@Id
@GeneratedValue
public final long getId(){
return id;
}
public void setId(long id){
this.id = id;
}
public String getFirstName(){
return firstName;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
}TestService.java (UserDao实现IUserDao并扩展GenericDao)
@Service(value = "testService")
@Transactional
public class TestService implements ITestService {
@Autowired
private IUserDao userDao;
@Transactional(readOnly = false)
public void saveUser(User newUser){
userDao.save(newUser);
}
.
.
.
}persistence.xml
<persistence-unit name="gamelistPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<!--
value='create' to build a new database on each run;
value='update' to modify an existing database;
value='create-drop' means the same as 'create' but also drops tables when Hibernate closes;
value='validate' makes no changes to the database
-->
</properties>
</persistence-unit>我没有任何错误或例外或任何东西。我能从我的数据库中阅读。只有更新、添加或删除不持久化。
以下是我所做的一切
任何帮助都是非常感谢的。提前感谢
发布于 2013-08-03 05:58:49
提交TX (或者刷新Hibernate)是失败的。声明性事务处理不起作用,或者声明需要进一步扩展。
不应该将DAO声明为事务性的吗?这就是实际操作发生的地方,您在TestService上的“事务性”声明实际上不会做任何事情--因此Spring可能会错过将HB注册到事务中。
我自己通常不使用声明式的,因为我觉得TX边界很简单&在用户活动上定义得很好--并且在显式编码(1行,通过我的助手类)时,更容易跟踪/更可靠。
坦率地说,我也不知道您希望如何处理DAO --如果您使用的是JPA/ Hibernate,那么这些层将处理DAO将做什么。我怀疑你是在用“无所事事”的类来充实你的应用程序,而这些类的正确用途/用途是你所不理解的。
怎么回事:
Session hb = HbHelper.session();
hb.save( user);
HbHelper.commit(); // commits via a TX.我使用这种样式与"OpenSessionInView“过滤器。
https://stackoverflow.com/questions/18027809
复制相似问题