0

私は休止状態の世界が初めてで、デモサンプルを準備しています。entityManager.persist(entity) 関数で試しました。エラーはありませんが、エンティティは DB に永続化されていません。

これが私のpersistence.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]

タックス、グンジャン。

4

2 に答える 2

0

EntityManager をフラッシュすることはありません。また、有効なトランザクションもありません。

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();
于 2012-05-22T12:47:58.810 に答える
0

最初にトランザクションを開始し、最後にトランザクションをコミットする必要があります。

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();
      }
   }
于 2012-05-22T12:49:30.260 に答える