2013-03-07 125 views
0

我正在使用spring hibernate & jpa開發一個項目,我正在將其部署到雲代工廠。我的問題是,當我打電話給Dao來堅持我的實體到MySQL數據庫時,沒有任何反應。沒有錯誤拋出,我試圖包裝堅持在一個try catch塊和什麼都沒有。春季休眠JPA不堅持雲代工

我將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 

任何反饋將不勝感激!

根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> 

服務:

@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); 


} 

DAO:

@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; 

回答

2

明白了!最後!

這裏是我必須做的改變的總結。

在Animal類(實體對象)中,我將id字段從Integer更改爲long。 (我懷疑這與修復有什麼關係!),並刪除了實現Serializable。

在根方面,我改變了TX從

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

<tx:annotation-driven/> 

和我在servlet上下文添加

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

然後我說

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

它看起來像是競爭者掃描之間的衝突。

爲什麼它在指向Oracle數據庫時工作我永遠不會知道。

0

嘗試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();   

} 
+0

謝謝你回到我身邊。我使用的是MySQL 5.1,所以我已經進行了您所推薦的更改,但仍然沒有持續。 – user2143767 2013-03-07 11:01:05

+0

它在本地工作嗎? – 2013-03-07 11:07:10

+0

我只能訪問本地機器上的oracle數據庫,但在對應用程序進行一些更改以指向oracle數據庫後,它完美地工作。 – user2143767 2013-03-07 11:12:33