I have problem with flush data to PostgreSQL database in SpringMVC. When i use entityManager.flush().
The Table is created in PosgreSQL, I can use pgadmin3 and it exists but the problem was that after i passed element from controller to the ImageDaoImpl it didn't push record to the database;/
@Repository
@Transactional(propagation = Propagation.REQUIRED)
public class ImageDaoImpl implements ImageDao {
@PersistenceContext
private EntityManager em;
@Transactional
public void register(Image image) {
em.persist(image);
em.flush();
return;
}
}
It threw me an error while i wanted to do register method.
javax.persistence.TransactionRequiredException: no transaction is in progress
I tried to do:
@Transactional
public void register(Image image) {
em.getTransaction().begin();
em.persist(image);
em.getTransaction().commit();
return;
}
but it threw me:
threw exception: java.lang.IllegalStateException: Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead
Firstly I thought that something is wrong and add fristly @PersistenceContext
.
I found EntityManager cannot use persist to save element to database
and tried to add
<tx:annotation-driven transaction-manager="transactionManager"/>
and
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
But I then i thought how it isn't possible to attach manager: Then i got error like this:
java.lang.ClassNotFoundException: org.springframework.jdbc.datasource.JdbcTransactionObjectSupport
What to do next? How i can integrate the <jee:jndi-lookup jndi-name="java:jboss/spring-quickstart/persistence" id="entityManagerFactory" ...
with the transactionManager?
What is the difference between them? Which one i should use to connect with database?
Maybe there is another problem which i couldn't see.
I have set all database connection but problem with flush made me crazy. I didn't try to read information from Entity in browser, before that i want to push information to database.
I read Spring jta-transaction-manager
and tried to comment that line
but it threw me: (MSC service thread 1-2) Context initialization failed: java.lang.NoClassDefFoundError: org/springframework/orm/jpa/JpaTransactionManager$JpaTransactionObject
Why it couldn't find the proper solution: EntityManager persist() method does not insert record to database Where should I register class Image to make the transaction?
Here is the fragment of the Controller.
@RequestMapping(value = "/save", method = RequestMethod.POST)
public String save(
@ModelAttribute("uploadForm") ImageDaoImpl uploadForm,
Model map, BindingResult result) throws IllegalStateException, IOException {
...
String filePath = fileCatalog + orgName;
Image memberImage = new Image();
memberImage.setFilePath(filePath);
imageDao.register(memberImage);
...
}
}
I create the object and set one value which i need in database. The file i get is save in the hard-drive. I need it later for my another program. It is good way to register object in the application. Maybe it is a reason that it isn't flush to the database?
At the end I am attaching my configurations files:
ImageDomain:
@Entity
@Table(name="IMAGES")
public class Image {
@Id
@Column(name="ID")
@GeneratedValue
private Integer id;
@Column(name="NAME")
private String name;
@Column(name="CONTENTTYPE")
private String contentType;
@Column(name="LENGTH")
private Integer length;
@Column(name="ISPOCESSED")
private boolean isprocessed;
@Column(name="CONTENT")
@Lob
private Blob content;
@Column(name="FILEPATH")
private String filepath;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setFilePath(String filepath) {
this.filepath = filepath;
}
public String getFilePath(){
return filepath;
}
public void setName(String name) {
this.name = name;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public Blob getContent() {
return content;
}
public void setContent(Blob content) {
this.content = content;
}
public Integer getLength() {
return length;
}
public void setLength(Integer length) {
this.length = length;
}
}
META-INF/spring/ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:component-scan base-package="org.springmvc.trophy.domain"/>
<context:component-scan base-package="org.springmvc.trophy.repo"/>
<tx:annotation-driven />
</beans>
META-INF/spring/infrastructure.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee.xsd">
<!-- JDNI name for EntityManagerFactory is defined in src/main/resources/META-INF/persistence.xml -->
<jee:jndi-lookup jndi-name="java:jboss/spring-quickstart/persistence" id="entityManagerFactory"
expected-type="javax.persistence.EntityManagerFactory" />
<bean id="entityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:jta-transaction-manager />
</beans>
META-INF/Persistance.xml
<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="primary" transaction-type="RESOURCE_LOCAL">
<!-- If you are running in a production environment, add a managed data
source, this example data source is just for development and testing! -->
<!-- The datasource is deployed as WEB-INF/spring-quickstart-ds.xml, you
can find it in the source at src/main/webapp/WEB-INF/spring-quickstart-ds.xml -->
<jta-data-source>java:jboss/datasources/ImagesDS</jta-data-source>
<properties>
<property name="jboss.entity.manager.factory.jndi.name"
value="java:jboss/spring-quickstart/persistence" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
webapp/WEB-INF/images-ds.xml
<?xml version="1.0" encoding="UTF-8"?>
<datasources xmlns="http://www.jboss.org/ironjacamar/schema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.jboss.org/ironjacamar/schema http://docs.jboss.org/ironjacamar/schema/datasources_1_0.xsd">
<!-- The datasource is bound into JNDI at this location. We reference this
in META-INF/persistence.xml -->
<datasource jndi-name="java:jboss/datasources/ImagesDS"
pool-name="kitchensink-quickstart" enabled="true" jta="false" use-java-context="true" use-ccm="false">
<connection-url>jdbc:postgresql://localhost:5432/dermadb</connection-url>
<driver-class>org.postgresql.Driver</driver-class>
<driver>postgresql-9.1-901.jdbc4.jar</driver>
<security>
<user-name>username</user-name>
<password>dbpassword</password>
</security>
</datasource>
</datasources>
I deployed module for PostgreSQL: in webapp/WEB-INF/jboss-deployment-structure.xml
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.0">
<deployment>
<dependencies>
<module name="org.postgresql"/>
<!-- <module name="com.h2database.h2"/> -->
<module name="org.codehaus.jackson.jackson-core-asl"/>
<module name="org.codehaus.jackson.jackson-mapper-asl"/>
<module name="org.slf4j"/>
</dependencies>
</deployment>
</jboss-deployment-structure>
Thanks in advance for the answer.