Skip to Main Content

Integration

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

I can not save new object using JPA entity manager, please help

912726Jan 23 2012 — edited Jan 23 2012
I am using Spring 3 with JPA to access database, I tried to persist object, but failed. This is what I do:
1) Config:

<tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/>
<bean id="txManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf" />
</bean>

<bean id="emf"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
<property name="loadTimeWeaver">
<bean
class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
<property name="persistenceUnitName" value="memberPU"></property>
</bean>


<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL" />
<property name="showSql" value="true" />
<property name="generateDdl" value="false" />
<property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/springinaction?createDatabaseIfNotExist\=true&amp;useUnicode\=true&amp;characterEncoding\=utf-8&amp;enableQueryTimeouts\=false" />
<property name="username" value="root" />
<property name="password" value="" />
<property name="initialSize" value="5" />
<property name="maxActive" value="10" />
</bean>

2) Persistence.xml:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
version="1.0">
<persistence-unit name="memberPU" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.hbm2ddl.auto" value="validate" />
</properties>
</persistence-unit>
</persistence>

3) Domain:


@Entity
@Table(name = "members")
public class Spitter {

@Id
@Column(name = "id")
@GeneratedValue(generator = "InvSeq")
@SequenceGenerator(name = "InvSeq", sequenceName = "group table seq01", allocationSize = 5)
private int id;

@Column
private String firstName;
@Column
private String lastName;
@Column
private int age;

gettters and setters...
}

4) Dao:

@Repository
@Transactional
public class SpitterDao {
@PersistenceContext
private EntityManager em;

public void addSpitter(Spitter spitter){

Spitter s = new Spitter();
//s.setId(null);
s.setFirstName("Frank");
s.setLastName("wqrewreqwr");
s.setAge(45);
System.out.print("Persisting into tDB here ");


this.em.merge(s);
this.em.persist(s);
//this.em.flush();
//this.em.getTransaction().commit();
}
}

If I do not call em.flush(), no error throws, and program is executed succesfully. BUT nothing is saved to DB. IF I used em.flush(), I got errormsg - "No Transaction", but the Dao is already annoted with @Transactional.

Reading through the JPA is no problem.

I tried different solutions heavily, but no result. Pleaes anyone point out what's the error and why object can not be serialized to DB.

Thanks very much
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 20 2012
Added on Jan 23 2012
1 comment
1,265 views