English 中文(简体)
Hiberate : 实体在持续() 函数中不会被推入 DB 。
原标题:Hibernate : entity is not pushed into DB in persist() function
  • 时间:2012-05-22 12:28:57
  •  标签:
  • hibernate

我是新来的冬眠世界 准备一个演示样本。 我尝试了实体管理者. perist( 实体) 功能 。 没有错误, 但实体没有持续到 DB 中 。

这是我的坚持. xml 文件...

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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_2_0.xsd">

    <persistence-unit name="NCLPEntity">

        <provider>org.hibernate.ejb.HibernatePersistence</provider>

        <!-- 
        <jta-data-source>nclp/oracle</jta-data-source>
         -->

        <class>com.fks.nclp.model.MapRoleModule</class>
        <class>com.fks.nclp.model.MstCityDtl</class>
        <class>com.fks.nclp.model.MstClpMcDtl</class>
        <class>com.fks.nclp.model.MstDiscSlbDtl</class>
        <class>com.fks.nclp.model.MstFormatDtl</class>
        <class>com.fks.nclp.model.MstGeneralDtl</class>
        <class>com.fks.nclp.model.MstMediaType</class>
        <class>com.fks.nclp.model.MstModuleDtl</class>
        <class>com.fks.nclp.model.MstRoleDtl</class>
        <class>com.fks.nclp.model.MstSiteDtl</class>
        <class>com.fks.nclp.model.MstStateDtl</class>
        <class>com.fks.nclp.model.MstUserDtl</class>

        <properties>           
            <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
            <property name="hibernate.connection.driver_class"  value="oracle.jdbc.driver.OracleDriver"/>
            <property name="hibernate.connection.url" value="jdbc:oracle:thin:@10.0.7.62:1521:CLP"/>
            <property name="hibernate.default_schema"  value="clp_orcl_db"/>
            <property name="hibernate.connection.username" value="CLPREM"/>
            <property name="hibernate.connection.password" value="********"/>           
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="false"/>
            <property name="hibernate.query.factory_class" value="org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory"/>
            <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/>
            <property name="hibernate.cache.use_second_level_cache" value="false"/>
            <property name="hibernate.order_inserts" value="true"/>
            <property name="hibernate.order_updates" value="true"/>
            <property name="hibernate.jdbc.batch_size" value = "30" />                     
        </properties>
    </persistence-unit>
</persistence>

这里是我测试持续功能的根根...

package com.fks.nclp.junit;

import java.math.BigDecimal;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import com.fks.nclp.model.MstStateDtl;

public class Test_1 {

    public static void main(String a[]){
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("NCLPEntity");
        EntityManager em = emf.createEntityManager();

        MstStateDtl state = new MstStateDtl();
        state.setStateId(1002);
        state.setZoneId(new BigDecimal("1234"));
        state.setDescription("testing");        

        em.persist(state);
        em.close();
        emf.close();
    }
}

这里是控制台外面...

May 22, 2012 5:43:40 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
May 22, 2012 5:43:40 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.1}
May 22, 2012 5:43:40 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
May 22, 2012 5:43:40 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
May 22, 2012 5:43:41 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
May 22, 2012 5:43:41 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
May 22, 2012 5:43:41 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: true
May 22, 2012 5:43:41 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [oracle.jdbc.driver.OracleDriver] at URL [jdbc:oracle:thin:@10.0.7.62:1521:CLP]
May 22, 2012 5:43:41 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=CLPREM, password=****, autocommit=true, release_mode=auto}
May 22, 2012 5:43:42 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
May 22, 2012 5:43:42 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
May 22, 2012 5:43:42 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000268: Transaction strategy: org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory
May 22, 2012 5:43:42 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
May 22, 2012 5:43:42 PM org.hibernate.validator.util.Version <clinit>
INFO: Hibernate Validator 4.1.0.Final
May 22, 2012 5:43:42 PM org.hibernate.validator.engine.resolver.DefaultTraversableResolver detectJPA
INFO: Instantiated an instance of org.hibernate.validator.engine.resolver.JPATraversableResolver.
May 22, 2012 5:43:42 PM org.hibernate.validator.engine.resolver.DefaultTraversableResolver detectJPA
INFO: Instantiated an instance of org.hibernate.validator.engine.resolver.JPATraversableResolver.
May 22, 2012 5:43:43 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH000030: Cleaning up connection pool [jdbc:oracle:thin:@10.0.7.62:1521:CLP]

萨克斯,贡詹。

最佳回答

您必须从头开始交易,然后在尾端进行交易。

public static void main(String args[])
   {
      EntityManager em;
      try
      {
          em = Persistence.createEntityManagerFactory(
            "NCLPEntity").createEntityManager();

         em.getTransaction().begin();


         MstStateDtl state = new MstStateDtl();
         state.setStateId(1002);
         state.setZoneId(new BigDecimal("1234"));
         state.setDescription("testing");
         em.persist(state);

         em.getTransaction().commit();

      }
      finally
      {
         if (em != null)
            em.close();
      }
   }
问题回答

你从不冲洗实体经理人,也没有任何交易。

EntityManager em = emf.createEntityManager();
em.getTransaction().begin();

MstStateDtl state = new MstStateDtl();
state.setStateId(1002);
state.setZoneId(new BigDecimal("1234"));
state.setDescription("testing");        
em.persist(state);
em.getTransaction().commit();  // performs an implicit flush

em.close();




相关问题
Multiple Hibernate instances using C3P0

I am facing a weird problem and it seems to be c3p0 related. I am starting two instances of an app in the same java vm which interact with each other. After some operations "APPARENT DEADLOCK" ...

Hibernate vs Ibatis caching

We can speed up a hibernate app easyly with 2nd level cache using infinispan or ehcache/terracotta,... but ibatis only have a simple interface to implement for caching. And hibernate knows more ...

Using annotations to implement a static join in hibernate

I m relatively new to hibernate and was wondering if someone could help me out. While I have no issues implementing a normal join on multiple columns in hibernate using the @JoinColumns tag, I m ...

Hibernate query with fetch question

I have a 2 entities in a One-To-Many relationship: OfficeView.java: public class OfficeView implements java.io.Serializable { private Integer officeId; private String addr1; private ...

hibernate interceptors : afterTransactionCompletion

I wrote a Hibernate interceptor : public class MyInterceptor extends EmptyInterceptor { private boolean isCanal=false; public boolean onSave(Object entity, Serializable arg1, Object[] arg2, String[]...

How to prevent JPA from rolling back transaction?

Methods invoked: 1. Struts Action 2. Service class method (annotated by @Transactional) 3. Xfire webservice call Everything including struts (DelegatingActionProxy) and transactions is configured ...

Hibernate/GORM: collection was not processed by flush()

I have an integration test in my Grails application that fails when I try to save an entity of type Member invitingMember.save(flush: true) This raises the following exception org.hibernate....

Hibernate Criteria API equivalent for "elements()"

Is it possible to implement the following query using Criteria API? select order from ORDER as order,ITEM as item where item.itemID like ITM_01 and item in elements(order.items)

热门标签