0

我有带有表单的简单网页。我插入值并将其发送到 db (addPage.html addPageController catalogDAo)。一切都很好,直到 CatalogDAO 中的 persist 方法。数据未插入到数据库(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>


持久性.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>



@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;
}
4

2 回答 2

0

您的@Transactional(propagation=SUPPORTS)意思是它将在事务中运行,如果存在的话,但除非在调用之前启动,否则没有事务。默认传播是REQUIRED,如果一个还没有存在,它将开始一个,如果有,将参与一个。

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

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManager" ref="entityManager"/>
</bean>
于 2013-07-19T17:10:09.207 回答
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();

}

}

于 2013-07-19T17:47:31.973 回答