2017-04-11 12 views
1

ここに私の簡単なhibernate.cfg.xmlファイルされる:テーブルを作成する前にhibernateがforeingキー制約を削除するのはなぜですか?

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-configuration PUBLIC 
"-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
    <session-factory> 
     <!-- Database connection settings --> 
     <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
     <property name="connection.url">jdbc:mysql://localhost/UserDB</property> 
     <property name="connection.username">root</property> 
     <property name="connection.password"></property> 

     <!-- JDBC connection pool (use the built-in) --> 
     <property name="connection.pool_size">1</property> 

     <!-- SQL dialect --> 
     <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> 

     <!-- Enable Hibernate's automatic session context management --> 
     <property name="current_session_context_class">thread</property> 

     <!-- Disable the second-level cache --> 
     <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> 

     <!-- Echo all executed SQL to stdout --> 
     <property name="show_sql">true</property> 
     <property name="format_sql">true</property> 

     <!-- Drop and re-create the database schema on startup --> 
     <property name="hbm2ddl.auto">create-drop</property> 

     <!-- Mapping files --> 
     <mapping class="com.stackoverflow.model.Person" /> 
     <mapping class="com.stackoverflow.model.Phone" /> 

    </session-factory> 

ここでは、単純なPersonエンティティである:ここで

@Entity(name = "Person") 
public class Person { 

    @Id 
    @GeneratedValue 
    private Long id; 

    public Person() { 
    } 
} 

は、シンプルな携帯電話のエンティティである:ここで

@Entity(name = "Phone") 
public class Phone { 

    @Id 
    @GeneratedValue 
    private Long id; 

    @Column(name = "`number`") 
    private String number; 

    @ManyToOne(cascade=CascadeType.ALL,optional=false) 
    @JoinColumn(name = "person_id", 
      foreignKey = @ForeignKey(name = "PERSON_ID_FK") 
    ) 
    private Person person; 

    public Phone() { 
    } 

    public Phone(String number) { 
     this.number = number; 
    } 

    public Long getId() { 
     return id; 
    } 

    public String getNumber() { 
     return number; 
    } 

    public Person getPerson() { 
     return person; 
    } 

    public void setPerson(Person person) { 
     this.person = person; 
    } 
} 

はしようとJUnitのテストクラスでありますこの関係をデータベースに保存する:

public class TestManyToOne { 
    private static SessionFactory sessionFactory; 
    private Session session; 

    @BeforeClass 
    public static void setUp() throws Exception { 
     // A SessionFactory is set up once for an application! 
     final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml") 
       .build(); 
     try { 
      sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory(); 
     } catch (Exception e) { 
      // The registry would be destroyed by the SessionFactory, but we had 
      // trouble building the SessionFactory 
      // so destroy it manually. 
      StandardServiceRegistryBuilder.destroy(registry); 
      System.out.println("Exception\n" + e); 
     } 
     assertNotNull(sessionFactory); 
    } 

    @AfterClass 
    public static void tearDown() throws Exception { 
     sessionFactory.close(); 
    } 

    @Before 
    public void beforeEach() { 
     session = sessionFactory.getCurrentSession(); 
     session.beginTransaction(); 

    } 

    @After 
    public void afterEach() { 
     session.getTransaction().commit(); 
     session.close(); 
    } 

    @Test 
    public void testSave() { 
     Person person = new Person(); 
     Phone phone1 = new Phone("93283287832"); 
     Phone phone2 = new Phone("0987654321"); 

     phone1.setPerson(person); 
     phone2.setPerson(person); 

     session.save(phone1); 
     session.save(phone2); 
    } 
} 

testSaveという@Testメソッドが1つしかありません。私はエラーを取得するだけで作成したデータベース上で実行する場合:エラーで

Hibernate: 

    alter table Phone 
     drop 
     foreign key PERSON_ID_FK 
апр 11, 2017 5:50:54 PM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection 
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.[email protected]6f2cfcc2] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode. 
апр 11, 2017 5:50:54 PM org.hibernate.tool.schema.internal.ExceptionHandlerLoggedImpl handleException 
WARN: GenerationTarget encountered exception accepting command : Error executing DDL via JDBC Statement 
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL via JDBC Statement 
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:62) 
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.applySqlString(SchemaDropperImpl.java:374) 
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.applySqlStrings(SchemaDropperImpl.java:359) 
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.applyConstraintDropping(SchemaDropperImpl.java:331) 
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.dropFromMetadata(SchemaDropperImpl.java:230) 
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.performDrop(SchemaDropperImpl.java:154) 
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.doDrop(SchemaDropperImpl.java:126) 
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.doDrop(SchemaDropperImpl.java:112) 
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:137) 
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:65) 
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:307) 
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:490) 
    at org.hibernate.boot.internal.MetadataImpl.buildSessionFactory(MetadataImpl.java:170) 
    at com.stackoverflow.dao.TestDao.setUp(TestDao.java:37) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) 
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309) 
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:283) 
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:173) 
Hibernate: 

    drop table if exists hibernate_sequence 
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153) 
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:128) 
    at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203) 
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155) 
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103) 
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'userdb.phone' doesn't exist 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406) 
    at com.mysql.jdbc.Util.getInstance(Util.java:381) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423) 
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936) 
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060) 
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2536) 
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2465) 
    at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:734) 
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:49) 
    ... 30 more 

この行は重要です:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'userdb.phone' doesn't exist 

はどのように休止伝えるためにテーブルを作成する前に、外部キー制約をドロップしませんか?

ありがとうございます。私は10に設定した後

答えて

0

<property name="connection.pool_size">10</property> 

私はそれを信じていなかったが、問題は、接続プールのサイズが1に設定されたことだったJUnitテストが正常に実行されますが、エラーは消えませんでした。それは日食によって無視されます。

関連する問題