【问题标题】:Spring Hibernate JPA not persisting cloud foundrySpring Hibernate JPA 不持久化云代工
【发布时间】:2013-03-07 10:42:27
【问题描述】:

我正在使用 spring hibernate 和 jpa 开发一个项目,并将其部署在 cloud Foundry 上。我的问题是,当我调用 Dao 将我的实体持久保存到 mysql 数据库时,什么也没有发生。没有抛出任何错误,我尝试将持久性包装在 try catch 块中,但什么也没有。

我将persistance.xml 中的show sql 属性设置为true。当我使用其他只查询数据库的 Dao 方法时,我可以看到运行的 SQL。但是当我尝试持久化时,没有 SQL 被写入控制台。

来自查询的示例控制台反馈

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.

来自持久化的控制台反馈示例:

INFO : com.lasthope.web.animals.service.AnimalsServiceImpl - Saving Gerry to database.
INFO : com.lasthope.web.animals.dao.AnimalsDaoImpl - DAO, saving animal Gerry ID: null

任何反馈将不胜感激!

根上下文.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> 

服务:

@Service("animalsService")
public class AnimalsServiceImpl implements AnimalsService {

@Autowired
private AnimalsDao animalsDao;


    @Override
@Transactional
public void saveAnimal(Animal animal) {

    logger.info("Saving "+animal.getName()+ " to database.");

    animalsDao.saveAnimal(animal);


}

道:

@Repository("animalsDao")
public class AnimalsDaoImpl implements AnimalsDao {

private static final Logger logger = LoggerFactory.getLogger(AnimalsDaoImpl.class);

   private EntityManager entityManager;

    public EntityManager getEntityManager() {
        return entityManager;
    }

    @PersistenceContext
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

@Override
public void saveAnimal(Animal animal) {

    logger.info("DAO, saving animal " +animal.getName() +" ID: " +animal.getAnimalId());    

    getEntityManager().persist(animal);         

}

persistance.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>

动物类:

@Entity
@Table(name="animals")
public class Animal implements Serializable  {

@Id
@Column(name = "ANIMAL_ID")
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer animalId;

@Column(name = "ANIMALTYPE")
private String animalType;

@Column(name = "BREED")
private String breed;

@Column(name = "NAME")
private String name;

@Column(name = "IMAGE_1")
@Lob
private Blob image1;

@Column(name = "IMAGE_1_CONTENT_TYPE")
private String image1ContentType;

@Column(name = "IMAGE_1_FILE_NAME")
private String image1FileName;

@Column(name = "IMAGE_1_FILE_SIZE")
private String image1FileSize;

@Column(name = "IMAGE_2")
@Lob
private Blob image2;

@Column(name = "IMAGE_2_CONTENT_TYPE")
private String image2ContentType;

@Column(name = "IMAGE_2_FILE_NAME")
private String image2FileName;

@Column(name = "IMAGE_2_FILE_SIZE")
private String image2FileSize;

@Column(name = "IMAGE_3")
@Lob
private Blob image3;

@Column(name = "IMAGE_3_CONTENT_TYPE")
private String image3ContentType;

@Column(name = "IMAGE_3_FILE_NAME")
private String image3FileName;

@Column(name = "IMAGE_3_FILE_SIZE")
private String image3FileSize;  

@Column(name = "ABOUT")
private String about;

@Column(name = "DATE_IN")
private Date dateIn;

@Column(name = "DATE_OUT")
private Date dateOut;

@Column(name = "STATUS")
private String status;

【问题讨论】:

    标签: spring hibernate jpa cloud-foundry object-persistence


    【解决方案1】:

    搞定了!终于!

    这是我必须做出的更改的摘要。

    在 Animal 类(实体对象)中,我将 id 字段从 Integer 更改为 long。 (我怀疑这与修复有什么关系!)并删除了可序列化的工具。

    在根上下文中,我从

    更改了 tx
    <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
    

    <tx:annotation-driven/>
    

    我加了

    <context:component-scan base-package="com.lasthope.web"/>
    

    然后在我的 servlet 上下文中我添加了

    <context:component-scan base-package="com.lasthope.web.controllers" />
    

    看起来这是竞争扫描之间的冲突。

    为什么它在指向我永远不会知道的 Oracle 数据库时起作用。

    【讨论】:

      【解决方案2】:

      尝试将GeneratedValue 策略设置为标识,确保将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" />
      

      尝试在持久化时创建事务:

      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();         
      
      }
      

      【讨论】:

      • 感谢您回复我。我正在使用 mySQL 5.1,所以我已经做出了你推荐的更改,但它仍然没有持久化。
      • 我只能访问本地机器上的 oracle 数据库,但是在我对应用程序进行一些更改以指向 oracle 数据库后,它可以正常工作。
      • 我之前尝试过,但收到以下错误:java.lang.IllegalStateException: Not allowed to create transaction on shared EntityManager - 请改用 Spring 事务或 EJB CMT
      • @user2143767 你有没有想过这个问题?
      • 还没有。我在本地机器上创建了一个 MySQL 数据库,并且存在同样的问题。
      猜你喜欢
      • 2011-09-22
      • 1970-01-01
      • 2012-11-29
      • 2013-11-12
      • 1970-01-01
      • 2011-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多