使用jpa无法在spring中将值保存到db

时间:2013-07-19 17:04:53

标签: spring spring-mvc jpa orm persist

我有简单的网页表格。我插入值并将其发送到db(addPage.html addPageController catalogDAo)。在CatalogDAO中,Everythings一直很好,直到持久化方法。数据不会插入到db(mysql,eclipselink,jpa.IDE - STS Eclipse)中。我试过谷歌和论坛的建议,但没有工作。我没有任何错误,页面加载正常,但坚持不。我不知道发生了什么事。有人有同样的事吗?谢谢! 这是我的代码:
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
  @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;
}

2 个答案:

答案 0 :(得分:0)

您的@Transactional(propagation=SUPPORTS)表示它将在事务中运行,如果存在,但除非在调用之前启动,否则没有事务。默认传播为REQUIRED,如果一个尚未存在,将启动一个,如果存在则将参与其中一个。

此外,似乎没有配置transactionManager。你可能想要使用以下内容;

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

答案 1 :(得分:0)

最终解决方案看起来:
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">


和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();

}

}