4
public void miseAJourTranscoIdCroisiere(Integer idCroisiereKaravel, String idCroisiereArmateur, TypeArmateur typeArmateur) {
        IdCroisiereKaravelArmateur idRelationship = new IdCroisiereKaravelArmateur();

        idRelationship.setIdCroisiereKaravel(idCroisiereKaravel);
        idRelationship.setIdCroisiereArmateur(idCroisiereArmateur);
        idRelationship.setTypeArmateur(typeArmateur);

        entityManager.persist(idRelationship);
    }

这个代码片段应该自己工作吗?它不会抛出异常。然而它什么也没做。我对 Java 的持久性感到很困惑。

因为我不能用 hql 插入 ... 值,所以我应该直接用 jpa 来做,对吧?

我试图以这种方式做到这一点,但我得到了这个

java.lang.IllegalStateException: Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:198)
    at $Proxy19.getTransaction(Unknown Source)
    at com.karavel.connectivity.gateway.croisiere.common.repository.impl.CatalogueKaravelRepositoryImpl.miseAJourTranscoIdCroisiere(CatalogueKaravelRepositoryImpl.java:56)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:196)
    at $Proxy23.miseAJourTranscoIdCroisiere(Unknown Source)
    at com.karavel.connectivity.gateway.croisiere.controller.service.RechercherCroisiereController.getItineraireCroisiere(RechercherCroisiereController.java:385)
    at com.karavel.connectivity.gateway.croisiere.controller.service.RechercherCroisiereController.obtenirListeCroisiere(RechercherCroisiereController.java:283)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.web.filter.AbstractRequestLoggingFilter.doFilterInternal(AbstractRequestLoggingFilter.java:193)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:291)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:662)

所以我考虑创建另一个 EntityManager 来执行此操作。但是,这不是我不能使用 EntityManagerFactory 的静态方法。

在ddbb中做一个简单的插入会这么乱吗?

如果我说愚蠢或混淆概念,我很抱歉,但我在 Java 持久性和 Hibernate 方面有两天的经验。

编辑以显示我的课程:

我确实需要一个事务,但我不能从共享的 EntityManager 中获得它(这是什么意思?)

我是否需要为该方法使用 EntityManager?如果我这样做,我不能使用 EntityManagerFactory 来保持静态。

对不起,这不是我创建的架构。我刚从 Spring 和 Hibernate 开始,所以这里有一个很大的组合……

应用上下文:

<bean id="gateway.croisiere.catalogueKaravel.dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${gateway.croisiere.catalogueKaravel.driver.className}" />
    <property name="url" value="${gateway.croisiere.catalogueKaravel.driver.url}" />
    <property name="username" value="${gateway.croisiere.catalogueKaravel.driver.username}" />
    <property name="password" value="${gateway.croisiere.catalogueKaravel.driver.password}" />
</bean>
<bean id="gateway.croisiere.catalogueKaravel.entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceXmlLocation" value="classpath:/catalogueKaravel-persistence.xml" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="generateDdl" value="${gateway.croisiere.catalogueKaravel.hibernate.hbm2ddl.auto}" />
            <property name="databasePlatform" value="${gateway.croisiere.catalogueKaravel.hibernate.dialect}" />
            <property name="showSql" value="${gateway.croisiere.catalogueKaravel.hibernate.show_sql}" />
        </bean>
    </property>
    <property name="dataSource" ref="gateway.croisiere.catalogueKaravel.dataSource" />
    <property name="persistenceUnitName" value="gateway-croisiere-catalogueKaravel-persitence-unit" />
</bean>
<bean id="gateway.croisiere.catalogueKaravel.transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="gateway.croisiere.catalogueKaravel.entityManagerFactory" />
    <property name="defaultTimeout" value="${gateway.croisiere.catalogueKaravel.hibernate.transaction.timeout}" />
</bean>
<tx:annotation-driven transaction-manager="gateway.croisiere.catalogueKaravel.transactionManager" />persistence:
<persistence 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_1_0.xsd" version="1.0">
    <persistence-unit name="gateway-croisiere-catalogueKaravel-persitence-unit" transaction-type="RESOURCE_LOCAL">
        <class>com.karavel.connectivity.gateway.croisiere.common.domain.cataloguekaravel.IdCroisiereKaravelArmateur</class>
        <class>com.karavel.connectivity.gateway.croisiere.common.domain.cataloguekaravel.CroisiereInfoCatalogueKaravel</class>
        <class>com.karavel.connectivity.gateway.croisiere.common.domain.cataloguekaravel.CroisiereOffreItineraireCatalogueKaravel</class>
        <class>com.karavel.connectivity.gateway.croisiere.common.domain.cataloguekaravel.EscaleCatalogueKaravel</class>
        <class>com.karavel.connectivity.gateway.croisiere.common.domain.cataloguekaravel.CroisiereItineraireCatalogueKaravel</class>
    </persistence-unit>
</persistence>

实体:

@Entity
@Table(name="transco_idcroisierekaravel_idcroisierearmateur")
public class IdCroisiereKaravelArmateur implements Serializable {

    private static final long serialVersionUID = -2764921376397148157L;

    @Id
    @GeneratedValue
    @Column(name="id")
    private Integer id;

    @Column(name="idCroisiereKaravel")
    private Integer idCroisiereKaravel;

    @Column(name="idCroisiereArmateur")
    private String idCroisiereArmateur;

    @Column(name="typeArmateur")
    @Enumerated(EnumType.STRING)
    private TypeArmateur typeArmateur;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getIdCroisiereKaravel() {
        return idCroisiereKaravel;
    }

    public void setIdCroisiereKaravel(Integer idCroisiereKaravel) {
        this.idCroisiereKaravel = idCroisiereKaravel;
    }

    public String getIdCroisiereArmateur() {
        return idCroisiereArmateur;
    }

    public void setIdCroisiereArmateur(String idCroisiereArmateur) {
        this.idCroisiereArmateur = idCroisiereArmateur;
    }

    public TypeArmateur getTypeArmateur() {
        return typeArmateur;
    }

    public void setTypeArmateur(TypeArmateur typeArmateur) {
        this.typeArmateur = typeArmateur;
    }
}

存储库

@SuppressWarnings("unchecked")
public class CatalogueKaravelRepositoryImpl implements CatalogueKaravelRepository {
    @PersistenceContext(unitName="gateway-croisiere-catalogueKaravel-persitence-unit")
    private EntityManager entityManager;

    @Override
    @Transactional
    public void miseAJourTranscoIdCroisiere(Integer idCroisiereKaravel, String idCroisiereArmateur, TypeArmateur typeArmateur) {

        IdCroisiereKaravelArmateur idRelationship = new IdCroisiereKaravelArmateur();

        idRelationship.setIdCroisiereKaravel(idCroisiereKaravel);
        idRelationship.setIdCroisiereArmateur(idCroisiereArmateur);
        idRelationship.setTypeArmateur(typeArmateur);

        EntityManager em = getEntityManager();

        // begin transaction 
        em.getTransaction().begin();

        // persist object - add to entity manager
        em.persist(idRelationship);

        // flush em - save to DB
        em.flush();

        // commit transaction at all
        em.getTransaction().commit();   
    }
}
4

3 回答 3

4

看起来你的问题很简单:它想要一个交易。您正在使用 Spring。将 Spring 拖入持久化领域的主要原因是获得基于 AOP 的注入事务.. ? 您应该能够注释您的方法 @Transaction 并在您的 applicationContext.xml 或通过其他一些引导程序启动 TransactionManager。

于 2012-05-25T14:04:26.863 回答
2

是的,EntityManager#persist应该可以保存记录,

  1. 您的实体管理器坚持似乎正在抛出运行时异常,将其包装在 try catch 块中

  2. 确保您的类在 spring 事务下声明,如果您使用基于注释的配置,那么您的类应标记为 @Transactional 并且您的 entityManager 应标记为 @PersistenceContext

于 2012-05-25T14:01:59.660 回答
1

正如问题的原始海报所述:


我的解决方案:

实际上 TranscationManager 的 bean 没有正确设置。我拿了在 Spring 配置文件中找到的第一个。由于审计系统工作正常,我只使用了相同的配置。但是,对于其他 DDBB 访问,它尝试使用 Audit TransactionManager(第一个出现在配置文件中)

我希望这可以帮助别人。很抱歉这样一个模糊的解决方案,但这是几个月前的事了,我无法再访问代码了。

于 2013-04-04T22:55:31.213 回答