2010-11-20 2 views
1

persistent()는 예외없이 w/o를 리턴하지만 엔티티는 데이터베이스에 저장되지 않습니다.Spring MVC 3.0.5 컨트롤러에서 JPA 엔티티가 데이터베이스에 유지되지 않음

@RequestMapping(method = RequestMethod.POST) 
public String form() { 
     EntityManager em = this.emf.createEntityManager(); 
     TaxRates t = new TaxRates(); 
     t.setCountry("US"); 
     // set more properties 
     em.persist(t); 
     em.close(); 
     ... 
} 

의 persistence.xml :

<?xml version="1.0" encoding="UTF-8"?> 
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> 
    <persistence-unit name="TT-SpringMVCPU" transaction-type="RESOURCE_LOCAL"> 
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> 
    ... 
    <class>com.sajee.db.TaxRates</class> 
    <exclude-unlisted-classes>true</exclude-unlisted-classes> 
    <properties> 
     <property name="javax.persistence.jdbc.url" value="jdbc:jtds:sqlserver://localhost:1234/mydb"/> 
     <property name="javax.persistence.jdbc.password" value="Password1"/> 
     <property name="javax.persistence.jdbc.driver" value="net.sourceforge.jtds.jdbc.Driver"/> 
     <property name="javax.persistence.jdbc.user" value="sa"/> 
    </properties> 
    </persistence-unit> 
</persistence> 

나는 어떤 트랜잭션 지원 또는 공상 기업의 기능 지원이 필요하지 않습니다. 엔티티를 만들고 데이터베이스에 저장하기 만하면됩니다.

어디로 잘못 가고 있습니까?

답변

2

persist()은 개체를 데이터베이스에 즉시 쓰지 않습니다. 대신 객체를 으로 표시하고 트랜잭션을 커밋하기 전 (또는 쿼리를 실행하기 전에 또는 명시 적으로 flush() 작업을 수행하는 동안) 데이터베이스에 쓰여지도록으로 표시합니다.

따라서 트랜잭션 동작이 필요하지 않은 경우에도 트랜잭션을 관리해야합니다. 다음과 같이 수동으로 수행 할 수 있습니다

@RequestMapping(method = RequestMethod.POST) 
public String form() { 
     EntityManager em = this.emf.createEntityManager(); 
     TaxRates t = new TaxRates(); 
     t.setCountry("US"); 
     // set more properties 
     em.getTransaction().begin(); 
     em.persist(t); 
     em.getTransaction().commit(); 
     em.close(); 
     ... 
} 

그러나 이 그것을 할 수있는 편리한 방법입니다.

관련 문제