English 中文(简体)
与实体管理器.persist (Object) 一起插入... 值;
原标题:Insert ... value with entityManager.persist(Object);
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);
    }

这个代码狙击手应该用它来操作吗? 它没有例外。 但是它什么也不做。 我和爪哇的持久性非常混为一谈。

既然我不能插入... 数值与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.CatalogueKaravel仓库仓库Impl.miseAJourTranscoIdCroisiere(CatalogueKaravel仓库仓库Impl.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)

于是我考虑为这样做而另设一个实体经理人。但是,我无法使用实体管理工具,这不是一个静态的方法。

能这么乱吗? 把一个简单的插插在三角帆上?

我很抱歉如果我说愚蠢或我混合概念, 但我有两天的经验 在爪哇持久性和休眠。

<强度> 编辑显示我的班级 :

我确实需要一笔交易, 但我不能从共同实体管理者那里得到这笔交易(这意味着什么? )

我需要一个实体管理者来做这个方法吗? 如果我需要,我就不能使用实体管理工具作为静态。

对不起,这建筑不是我创造的,我刚从春天和休眠开始,所以这里有一大堆东西要给我...

应用文本 :

<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 CatalogueKaravel仓库仓库Impl implements CatalogueKaravel仓库仓库 {
    @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();   
    }
}
最佳回答

正如问题原海报上所说的:


My solution:

事实上, TranscationManager 的豆类没有正确设置。 我选择了在 Spring 配置文件中发现的第一个豆类。 由于审计系统运作良好, 我刚刚使用了同样的配置。 但是, 对于其他的 DDBB 访问, 它试图使用审计交易管理器( 首个出现在配置文件中) 。

但几个月前的事了,我无法再使用密码了。

问题回答

看起来你的问题很简单: 它想要一个交易。 您正在使用 Spring 。 将 Spring 拖入持久性域的主要原因是要获得 AOP 的注射交易 。? 您应该能够批注您的方法@ Transaction 并启动交易管理器, 无论是在您的应用程序Context. xml 中, 还是通过其他的陷阱 。

是的, EntyManager #persist 应该能够保存记录,

  1. 您的实体经理似乎一直坚持 正在扔运行时间例外 把它包起来 尝试抓捕布洛克

  2. 如果您正在使用基于批注的配置, 确保在春季交易中宣布您的类别, 那么您的类别应该被标记为@ Transactional, 您的实体管理器应该被标记为@ PersistenceContext





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签