Question

I have simple web page with form. I insert values and send it to db (addPage.html addPageController catalogDAo). Everythings are fine till persist method in CatalogDAO. The data are not insert in to db (mysql, eclipselink, jpa. IDE - STS Eclipse). I tried advices from google and forum but didn' work. I don't have any errors, pages are loading fine but persist no. I don't know what is going on. Have someone got same isse? Thanks! Here is my codes:
servlet-context.xml

      <?xml version="1.0" encoding="UTF-8"?>
     <beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    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/aop http://www.springframework.org/schema/aop/spring-aop-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="foo" />
<annotation-driven />
<resources mapping="/css/**" location="/css/" />
<resources mapping="/js/libs/**" location="/js/libs/" />
<resources mapping="/img/**" location="/img/" />
    <beans:bean id="localeResolver"
    class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
    <beans:property name="defaultLocale" value="en_US" />
</beans:bean>
<beans:bean id="localeChangeInterceptor"
    class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
    <beans:property name="paramName" value="language" />
</beans:bean>
<beans:bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" >
    <beans:property name="interceptors">
       <beans:list>
        <beans:ref bean="localeChangeInterceptor" />
       </beans:list>
    </beans:property>
</beans:bean>
<beans:bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <beans:property name="basename" value="locale/messages" />
</beans:bean>
<beans:bean id="templateResolver"
    class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
    <beans:property name="prefix" value="/web-inf/views/" />
    <beans:property name="suffix" value=".html" />
    <beans:property name="templateMode" value="HTML5" />
</beans:bean>
<beans:bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
    <beans:property name="templateResolver" ref="templateResolver" />
</beans:bean>
<beans:bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
    <beans:property name="templateEngine" ref="templateEngine" />
</beans:bean>
<beans:bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <beans:property name="url" value="jdbc:mysql://localhost:3306/katalog" />
    <beans:property name="username" value="root" />
    <beans:property name="password" value="1977" />
</beans:bean>
    <beans:bean id="emf"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <beans:property name="persistenceUnitName" value="eCommerce" />
</beans:bean>
<tx:annotation-driven/>
   </beans:beans>


persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="eCommerce">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>foo.components.Catalog</class>
<properties>
  <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/katalog"/>
  <property name="javax.persistence.jdbc.password" value="1977"/>
  <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
  <property name="javax.persistence.jdbc.user" value="root"/>
  <property name="eclipselink.ddl-generation" value="create-tables"/>
</properties>
</persistence-unit>


DAO

@Component
@Transactional(propagation = Propagation.SUPPORTS)
public class CatalogDAO {
@PersistenceContext private EntityManager em;

public void zapisz(Catalog strona) {
 em.persist(strona);
      }
public Catalog getStrone(int Id){
    return em.find(Catalog.class, Id);
}
public List<Catalog> getAllStrony() {
    TypedQuery<Catalog> query = em.createQuery(
       "SELECT c FROM Catalog c ORDER BY c.id", Catalog.class);
    return query.getResultList();

}

}
Entity

  @Entity
  @Table(name="catalog")
  @NamedQuery(name="Catalog.findAll", query="SELECT c FROM Catalog c")
  public class Catalog implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="Id", unique=true)

private long Id;

private String adres;

private String opis;

private String tytul;

public Catalog() {
}

public long getId() {
    return this.Id;
}

public void setId(long Id) {
    this.Id = Id;
}

public String getAdres() {
    return this.adres;
}

public void setAdres(String adres) {
    this.adres = adres;
}

public String getOpis() {
    return this.opis;
}

public void setOpis(String opis) {
    this.opis = opis;
}

public String getTytul() {
    return this.tytul;
}

public void setTytul(String tytul) {
    this.tytul = tytul;
}
Was it helpful?

Solution

your @Transactional(propagation=SUPPORTS) means that it will run in a transaction, if one is present, but unless one is started before invocation, there is no transaction. the default propagation is REQUIRED which will start one if one is not already present, and will participate in one if there is.

also, it doesn't appear that there's a transactionManager configured. you may want to use something like the following;

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManager" ref="entityManager"/>
</bean>

OTHER TIPS

The final solutions looks:
servlet-context.xml:

<beans:bean id="emf"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <beans:property name="dataSource" ref="dataSource" />
    <beans:property name="persistenceUnitName" value="eCommerce" />
</beans:bean>

<beans:bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">


and DAO:

@Component
@Transactional
public class CatalogDAO {

@PersistenceContext private EntityManager em;

public void zapisz(Catalog strona) {

    em.persist(strona);
        }
public Catalog getStrone(int Id){
    return em.find(Catalog.class, Id);
}
public List<Catalog> getAllStrony() {
    TypedQuery<Catalog> query = em.createQuery(
       "SELECT c FROM Catalog c ORDER BY c.id", Catalog.class);
    return query.getResultList();

}

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top