2011-06-28 11 views
0

私は、hibernate JPA 2 + springを使用してWebアプリケーションを構築します。私はドメインモデルの作成に問題があります。永続性では、エンティティからデータベーステーブルの自動作成を宣言しました。ここに私のpersistence.xmlファイルがあります:永続性はデータベース内のエンティティからテーブルを作成しません

<persistence-unit name="basicPersistenceUnit" transaction-type="RESOURCE_LOCAL"> 
    <provider>org.hibernate.ejb.HibernatePersistence</provider> 
    <properties> 
     <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/> 
     <property name="hibernate.hbm2ddl.auto" value="create"/> 
     <property name="hibernate.connection.charSet" value="UTF-8"/> 
    </properties> 
</persistence-unit> 

私は2つのクラスを持っています。一つは継承されたクラスに永続性を提供する抽象的です:ここで

import org.hibernate.annotations.GenericGenerator; 
import org.hibernate.type.UUIDBinaryType; 
import javax.persistence.*; 

@MappedSuperclass 
abstract class AbstractDomainObject implements DomainObject { 

    private static final long serialVersionUID = 1L; 

    private UUIDBinaryType id; 

    private Long version; 

    @Id 
    @GeneratedValue(generator = "system-uuid") 
    @GenericGenerator(name = "system-uuid", strategy = "uuid") 
    @Column(length = 16, unique = true, nullable = false) 
    public final UUIDBinaryType getId() { 
     return id; 
    } 

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

    @Version 
    public final Long getVersion() { 
     return version; 
    } 

    public final void setVersion(Long version) { 
     this.version = version; 
    } 

    @Override 
    public boolean equals(Object o) { 
     if (this == o) return true; 
     if (!(o instanceof AbstractDomainObject)) return false; 

     AbstractDomainObject that = (AbstractDomainObject) o; 

     return !(id != null ? !id.equals(that.id) : that.id != null) && 
       !(version != null ? !version.equals(that.version) : that.version != null); 
    } 

    @Override 
    public int hashCode() { 
     int result = id != null ? id.hashCode() : 0; 
     result = 31 * result + (version != null ? version.hashCode() : 0); 
     return result; 
    } 

} 

は、エンティティを表す具体的なクラスである:ここでは

import javax.persistence.Entity; 

@Entity 
public class Person extends AbstractDomainObject { 

    private static final long serialVersionUID = 1L; 

    private String name; 

    private String surname; 

    private Integer identifier; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getSurname() { 
     return surname; 
    } 

    public void setSurname(String surname) { 
     this.surname = surname; 
    } 

    public Integer getIdentifier() { 
     return identifier; 
    } 

    public void setIdentifier(Integer identifier) { 
     this.identifier = identifier; 
    } 

    @Override 
    public boolean equals(Object o) { 
     if (this == o) return true; 
     if (!(o instanceof Person)) return false; 
     if (!super.equals(o)) return false; 

     Person test = (Person) o; 

     return !(name != null ? !name.equals(test.name) : test.name != null) && 
       !(surname != null ? !surname.equals(test.surname) : test.surname != null) && 
       !(identifier != null ? !identifier.equals(test.identifier) : test.identifier != null); 

    } 

    @Override 
    public int hashCode() { 
     int result = super.hashCode(); 
     result = 31 * result + (name != null ? name.hashCode() : 0); 
     result = 31 * result + (surname != null ? surname.hashCode() : 0); 
     result = 31 * result + (identifier != null ? identifier.hashCode() : 0); 
     return result; 
    } 

} 

は、私は抽象クラスで使用しているインタフェースです。

import org.hibernate.type.UUIDBinaryType; 
import java.io.Serializable; 

public interface DomainObject extends Serializable { 

    UUIDBinaryType getId(); 

    void setId(UUIDBinaryType id); 

    Long getVersion(); 

    void setVersion(Long version); 

    boolean equals(Object o); 

    int hashCode(); 

} 

そしてここでは、デバッグモードでのTomcatからのログです:

Jun 28, 2011 6:23:32 PM org.apache.catalina.startup.HostConfig deployWAR 
INFO: Deploying web application archive webFrontend-1.0-SNAPSHOT.war 
2011-06-28 18:23:33,495 [Thread-29] INFO org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization started 
2011-06-28 18:23:33,530 [Thread-29] INFO org.springframework.web.context.support.XmlWebApplicationContext - Refreshing Root WebApplicationContext: startup date [Tue Jun 28 18:23:33 CEST 2011]; root of context hierarchy 
2011-06-28 18:23:33,583 [Thread-29] INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from URL [file:/opt/apache-tomcat-7.0.16/webapps/webFrontend-1.0-SNAPSHOT/WEB-INF/classes/META-INF/spring/applicationContext.xml] 
2011-06-28 18:23:33,709 [Thread-29] INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from URL [file:/opt/apache-tomcat-7.0.16/webapps/webFrontend-1.0-SNAPSHOT/WEB-INF/classes/META-INF/spring/basicDataSource.xml] 
2011-06-28 18:23:33,722 [Thread-29] INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from URL [file:/opt/apache-tomcat-7.0.16/webapps/webFrontend-1.0-SNAPSHOT/WEB-INF/classes/META-INF/spring/applicationContext-persistence.xml] 
2011-06-28 18:23:33,909 [Thread-29] INFO org.springframework.beans.factory.config.PropertyPlaceholderConfigurer - Loading properties file from file [/opt/apache-tomcat-7.0.16/webapps/webFrontend-1.0-SNAPSHOT/WEB-INF/classes/META-INF/spring/database.properties] 
2011-06-28 18:23:33,929 [Thread-29] INFO org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.s[email protected]2863725d: defining beans [org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,basicDataSource,PersonDao,org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor#0,entityManagerFactory,transactionManager,org.springframework.transaction.config.internalTransactionAspect]; root of factory hierarchy 
2011-06-28 18:23:33,995 [Thread-29] INFO org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean - Building JPA container EntityManagerFactory for persistence unit 'basicPersistenceUnit' 
2011-06-28 18:23:34,153 [Thread-29] INFO org.hibernate.annotations.common.Version - Hibernate Commons Annotations 3.2.0.Final 
2011-06-28 18:23:34,160 [Thread-29] INFO org.hibernate.cfg.Environment - Hibernate 3.6.3.Final 
2011-06-28 18:23:34,161 [Thread-29] INFO org.hibernate.cfg.Environment - hibernate.properties not found 
2011-06-28 18:23:34,164 [Thread-29] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist 
2011-06-28 18:23:34,167 [Thread-29] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling 
2011-06-28 18:23:34,243 [Thread-29] INFO org.hibernate.ejb.Version - Hibernate EntityManager 3.6.3.Final 
2011-06-28 18:23:34,261 [Thread-29] INFO org.hibernate.ejb.Ejb3Configuration - Processing PersistenceUnitInfo [ 
    name: basicPersistenceUnit 
    ...] 
2011-06-28 18:23:34,359 [Thread-29] INFO org.hibernate.cfg.Configuration - Hibernate Validator not found: ignoring 
2011-06-28 18:23:34,373 [Thread-29] INFO org.hibernate.cfg.search.HibernateSearchEventListenerRegister - Unable to find org.hibernate.search.event.FullTextIndexEventListener on the classpath. Hibernate Search is not enabled. 
2011-06-28 18:23:34,376 [Thread-29] INFO org.hibernate.connection.ConnectionProviderFactory - Initializing connection provider: org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider 
2011-06-28 18:23:34,378 [Thread-29] INFO org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider - Using provided datasource 
2011-06-28 18:23:34,666 [Thread-29] INFO org.hibernate.dialect.Dialect - Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect 
2011-06-28 18:23:34,717 [Thread-29] INFO org.hibernate.cfg.SettingsFactory - Database -> 
     name : MySQL 
    version : 5.0.51a-24+lenny5 
     major : 5 
     minor : 0 
2011-06-28 18:23:34,722 [Thread-29] INFO org.hibernate.cfg.SettingsFactory - Driver -> 
     name : MySQL-AB JDBC Driver 
    version : mysql-connector-java-5.1.16 (Revision: ${bzr.revision-id}) 
     major : 5 
     minor : 1 
2011-06-28 18:23:34,723 [Thread-29] INFO org.hibernate.transaction.TransactionFactoryFactory - Transaction strategy: org.hibernate.transaction.JDBCTransactionFactory 
2011-06-28 18:23:34,725 [Thread-29] INFO org.hibernate.transaction.TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended) 
2011-06-28 18:23:34,725 [Thread-29] INFO org.hibernate.cfg.SettingsFactory - Automatic flush during beforeCompletion(): disabled 
2011-06-28 18:23:34,725 [Thread-29] INFO org.hibernate.cfg.SettingsFactory - Automatic session close at end of transaction: disabled 
2011-06-28 18:23:34,725 [Thread-29] INFO org.hibernate.cfg.SettingsFactory - JDBC batch size: 15 
2011-06-28 18:23:34,725 [Thread-29] INFO org.hibernate.cfg.SettingsFactory - JDBC batch updates for versioned data: disabled 
2011-06-28 18:23:34,726 [Thread-29] INFO org.hibernate.cfg.SettingsFactory - Scrollable result sets: enabled 
2011-06-28 18:23:34,726 [Thread-29] INFO org.hibernate.cfg.SettingsFactory - JDBC3 getGeneratedKeys(): enabled 
2011-06-28 18:23:34,726 [Thread-29] INFO org.hibernate.cfg.SettingsFactory - Connection release mode: auto 
2011-06-28 18:23:34,726 [Thread-29] INFO org.hibernate.cfg.SettingsFactory - Maximum outer join fetch depth: 2 
2011-06-28 18:23:34,726 [Thread-29] INFO org.hibernate.cfg.SettingsFactory - Default batch fetch size: 1 
2011-06-28 18:23:34,726 [Thread-29] INFO org.hibernate.cfg.SettingsFactory - Generate SQL with comments: disabled 
2011-06-28 18:23:34,726 [Thread-29] INFO org.hibernate.cfg.SettingsFactory - Order SQL updates by primary key: disabled 
2011-06-28 18:23:34,726 [Thread-29] INFO org.hibernate.cfg.SettingsFactory - Order SQL inserts for batching: disabled 
2011-06-28 18:23:34,726 [Thread-29] INFO org.hibernate.cfg.SettingsFactory - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory 
2011-06-28 18:23:34,728 [Thread-29] INFO org.hibernate.hql.ast.ASTQueryTranslatorFactory - Using ASTQueryTranslatorFactory 
2011-06-28 18:23:34,728 [Thread-29] INFO org.hibernate.cfg.SettingsFactory - Query language substitutions: {} 
2011-06-28 18:23:34,728 [Thread-29] INFO org.hibernate.cfg.SettingsFactory - JPA-QL strict compliance: enabled 
2011-06-28 18:23:34,728 [Thread-29] INFO org.hibernate.cfg.SettingsFactory - Second-level cache: enabled 
2011-06-28 18:23:34,728 [Thread-29] INFO org.hibernate.cfg.SettingsFactory - Query cache: disabled 
2011-06-28 18:23:34,728 [Thread-29] INFO org.hibernate.cfg.SettingsFactory - Cache region factory : org.hibernate.cache.impl.NoCachingRegionFactory 
2011-06-28 18:23:34,729 [Thread-29] INFO org.hibernate.cfg.SettingsFactory - Optimize cache for minimal puts: disabled 
2011-06-28 18:23:34,730 [Thread-29] INFO org.hibernate.cfg.SettingsFactory - Structured second-level cache entries: disabled 
2011-06-28 18:23:34,733 [Thread-29] INFO org.hibernate.cfg.SettingsFactory - Echoing all SQL to stdout 
2011-06-28 18:23:34,734 [Thread-29] INFO org.hibernate.cfg.SettingsFactory - Statistics: disabled 
2011-06-28 18:23:34,734 [Thread-29] INFO org.hibernate.cfg.SettingsFactory - Deleted entity synthetic identifier rollback: disabled 
2011-06-28 18:23:34,734 [Thread-29] INFO org.hibernate.cfg.SettingsFactory - Default entity-mode: pojo 
2011-06-28 18:23:34,734 [Thread-29] INFO org.hibernate.cfg.SettingsFactory - Named query checking : enabled 
2011-06-28 18:23:34,734 [Thread-29] INFO org.hibernate.cfg.SettingsFactory - Check Nullability in Core (should be disabled when Bean Validation is on): enabled 
2011-06-28 18:23:34,747 [Thread-29] INFO org.hibernate.impl.SessionFactoryImpl - building session factory 
2011-06-28 18:23:34,753 [Thread-29] INFO org.hibernate.type.BasicTypeRegistry - Type registration [wrapper_characters_clob] overrides previous : [email protected] 
2011-06-28 18:23:34,753 [Thread-29] INFO org.hibernate.type.BasicTypeRegistry - Type registration [characters_clob] overrides previous : [email protected] 
2011-06-28 18:23:34,753 [Thread-29] INFO org.hibernate.type.BasicTypeRegistry - Type registration [clob] overrides previous : [email protected] 
2011-06-28 18:23:34,753 [Thread-29] INFO org.hibernate.type.BasicTypeRegistry - Type registration [java.sql.Clob] overrides previous : [email protected] 
2011-06-28 18:23:34,753 [Thread-29] INFO org.hibernate.type.BasicTypeRegistry - Type registration [blob] overrides previous : [email protected] 
2011-06-28 18:23:34,754 [Thread-29] INFO org.hibernate.type.BasicTypeRegistry - Type registration [java.sql.Blob] overrides previous : org.hiberna[email protected] 
2011-06-28 18:23:34,754 [Thread-29] INFO org.hibernate.type.BasicTypeRegistry - Type registration [materialized_clob] overrides previous : [email protected] 
2011-06-28 18:23:34,754 [Thread-29] INFO org.hibernate.type.BasicTypeRegistry - Type registration [wrapper_materialized_blob] overrides previous : [email protected] 
2011-06-28 18:23:34,754 [Thread-29] INFO org.hibernate.type.BasicTypeRegistry - Type registration [materialized_blob] overrides previous : [email protected] 
2011-06-28 18:23:34,803 [Thread-29] INFO org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured 
2011-06-28 18:23:34,822 [Thread-29] INFO org.hibernate.tool.hbm2ddl.SchemaExport - Running hbm2ddl schema export 
2011-06-28 18:23:34,822 [Thread-29] INFO org.hibernate.tool.hbm2ddl.SchemaExport - exporting generated schema to database 
2011-06-28 18:23:34,827 [Thread-29] INFO org.hibernate.tool.hbm2ddl.SchemaExport - schema export complete 
2011-06-28 18:23:34,934 [Thread-29] INFO org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 1438 ms 
2011-06-28 18:23:34,962 [Thread-29] INFO org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'webFrontendDispatcher': initialization started 
2011-06-28 18:23:34,965 [Thread-29] INFO org.springframework.web.context.support.XmlWebApplicationContext - Refreshing WebApplicationContext for namespace 'webFrontendDispatcher-servlet': startup date [Tue Jun 28 18:23:34 CEST 2011]; parent: Root WebApplicationContext 
2011-06-28 18:23:34,967 [Thread-29] INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/spring/web-config.xml] 
2011-06-28 18:23:35,083 [Thread-29] INFO org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.s[email protected]391c3288: defining beans [org.springframework.context.annotation.CommonAnnotationBeanPostProcessor#0,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.config.viewControllerHandlerAdapter,org.springframework.web.servlet.config.viewControllerHandlerMapping]; parent: org.s[email protected]2863725d 
2011-06-28 18:23:35,299 [Thread-29] INFO org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped URL path [/index.jsp] onto handler of type [class org.springframework.web.servlet.mvc.ParameterizableViewController] 
2011-06-28 18:23:35,385 [Thread-29] INFO org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'webFrontendDispatcher': initialization completed in 422 ms 

このログにはエラーはなく、スキーマのエクスポートに関する情報は完全ですが、テーブルPersonはデータベースに作成されていません。私は間違った方法で何かをしますか?

+0

@Columnで注釈を付けるようにしてください(私は知っている必要はありません) – Bozho

答えて

0

<prop key="hibernate.hbm2ddl.auto">update</prop> 

それはecistingスキーマを更新します試してみて、それの新しいものは存在しません作成してください。

私もMySQLを使っていますが、問題はありません。

+0

私は試しました: <プロパティ名= "hibernate.hbm2ddl.auto" value = "update" /> しかし失敗しました –

0

私はこの問題を解決します。@Entityアノテーションを持つクラスのクラス名を含むタグをpersistence.xmlファイルに追加します。しかし、なぜ私はこのようにクラスを追加する必要がありますか?私がコードを生成するスプリングロウでは、persistence.xmlファイルにタグはありません

+0

RooがORM.xmlファイルを使用しているかどうか確認してください –

0

私は、MySQLの予約語であるエンティティ "Group"(私が使っていたデータベース:https://dev.mysql.com/doc/refman/5.5/en/reserved-words.html)を付けたので、この問題がありました。私は自分のエンティティ/テーブル/ JSONオブジェクトのために別の名前を選択しなければなりませんでした。

o.h.SQL: drop table if exists Group 
o.h.t.h.SchemaExport: HHH000389: Unsuccessful: drop table if exists Group 
o.h.t.h.SchemaExport: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Group' at line 1 

Hibernateが問題にそれ以外の場合は黙っていた予約語は、沼のビットであるため、Hibernateのドライバーがそれを拾う必要がありますが、私は思う...:次のように私は、起動時にHibernateの出力でこれを見ることができました。

注:

休止出力は次のように定義される:最終的に予約語を決定

hibernate.show_sql : true 

と休止状態の方言(このように定義される:

hibernate.dialect: org.hibernate.dialect.MySQL5InnoDBDialect 
関連する問題