Spring Hibernate JPA未持久化云计算
我正在使用SpringHibernate&jpa进行一个项目,并将其部署在CloudFoundry上。我的问题是,当我调用Dao将我的实体持久化到mysql数据库时,什么都没有发生。没有抛出错误,我已经尝试将persist包装在try-catch块中,但什么都没有 我已将persistence.xml中的show sql属性设置为true。当我使用其他Dao方法查询数据库时,我可以看到运行的SQL。但是当我尝试持久化时,没有SQL被写入控制台 来自查询的控制台反馈示例Spring Hibernate JPA未持久化云计算,spring,hibernate,jpa,cloud-foundry,object-persistence,Spring,Hibernate,Jpa,Cloud Foundry,Object Persistence,我正在使用SpringHibernate&jpa进行一个项目,并将其部署在CloudFoundry上。我的问题是,当我调用Dao将我的实体持久化到mysql数据库时,什么都没有发生。没有抛出错误,我已经尝试将persist包装在try-catch块中,但什么都没有 我已将persistence.xml中的show sql属性设置为true。当我使用其他Dao方法查询数据库时,我可以看到运行的SQL。但是当我尝试持久化时,没有SQL被写入控制台 来自查询的控制台反馈示例 Hibernate: se
Hibernate: select animal0_.animal_id as animal1_1_, animal0_.about as about1_, animal0_.animaltype as animaltype1_, animal0_.breed as breed1_, animal0_.date_in as date5_1_, animal0_.date_out as date6_1_, animal0_.image_1 as image7_1_, animal0_.image_1_content_type as image8_1_, animal0_.image_1_file_name as image9_1_, animal0_.image_1_file_size as image10_1_, animal0_.image_2 as image11_1_, animal0_.image_2_content_type as image12_1_, animal0_.image_2_file_name as image13_1_, animal0_.image_2_file_size as image14_1_, animal0_.image_3 as image15_1_, animal0_.image_3_content_type as image16_1_, animal0_.image_3_file_name as image17_1_, animal0_.image_3_file_size as image18_1_, animal0_.name as name1_, animal0_.status as status1_ from animals animal0_
INFO : com.lasthope.web.animals.service.AnimalsServiceImpl - Found 0 animals in care.
来自persist的控制台反馈示例:
INFO : com.lasthope.web.animals.service.AnimalsServiceImpl - Saving Gerry to database.
INFO : com.lasthope.web.animals.dao.AnimalsDaoImpl - DAO, saving animal Gerry ID: null
任何反馈都将不胜感激
root-context.xml:
<cloud:data-source id="dataSource" />
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
<property name="dataSource" ref="dataSource"/>
</bean>
道:
persistence.xml
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
尝试将
GeneratedValue
策略设置为identity,确保ANIMAL_ID
列指定为自动编号
@Id
@Column(name = "ANIMAL_ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer animalId;
此外,如果您使用的是新版本的mySql(v5.x+),那么在persistence.xml文件中,您应该将方言指定为:
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
开始工作了!终于 下面是我不得不做的更改的摘要 在Animal类(实体对象)中,我将id字段从整数改为长字符。(我怀疑这与修复程序有关!)并删除了可序列化的工具 在根上下文中,我从
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
到
我补充说
<context:component-scan base-package="com.lasthope.web"/>
然后在我的servlet上下文中添加了
<context:component-scan base-package="com.lasthope.web.controllers" />
这看起来像是compenet扫描之间的冲突
为什么它在指向Oracle数据库时起作用,我永远也不知道。谢谢您回复我。我使用的是mySQL 5.1,所以我已经做了您建议的更改,但仍然无法持久。我只能访问本地计算机上的oracle数据库,但是,在我对应用程序进行一些更改以指向oracle数据库之后,它工作得非常好。我以前也尝试过这样做,但出现以下错误:java.lang.IllegalStateException:不允许在共享EntityManager上创建事务-使用Spring事务或EJB CMTinstead@user2143767你有没有想过?还没有。我在本地机器上创建了一个MySQL数据库,同样的问题也存在。
public void saveAnimal(Animal animal) {
logger.info("DAO, saving animal " +animal.getName() +" ID: " +animal.getAnimalId());
EntityManager em = getEntityManager();
em.getTransaction().begin();
em.persist(animal);
em.getTransaction().commit();
}
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
<tx:annotation-driven/>
<context:component-scan base-package="com.lasthope.web"/>
<context:component-scan base-package="com.lasthope.web.controllers" />