Hibernate不会生成带有注释的表 [英] Hibernate does not generate table with annotations

查看:128
本文介绍了Hibernate不会生成带有注释的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Wicket与Spring和Hibernate结合使用,至少那是我要尝试的问题,问题在于自动生成带有Hibernate批注的表.

I'm using Wicket in combination with Spring and Hibernate, at least that's what I'm trying to do, the problem comes with auto generating the tables with Hibernate annotations.

我一直在尝试对配置进行许多更改,但似乎无法弄清楚为什么我的配置不生成任何表.而且我希望有人可以向我指出正确的方向,即使我不确定Spring的配置也是如此.

I've been trying many changes in the configuration but can't seem to figure out why my configuration doesn't generate any tables. And I'm hoping someone can point me in the right direction, even about the Spring configuration I'm not sure.

我已将我要使用的所有文件都包含在链接中,以使它不会是一长串的配置文件.

I've included all the files I'm using to try to make this work in links, so that it won't be a very long list of configuration files.

我正在使用带注释的以下类, http://schrealex.com/downloads/User. java :

I'm using the following class with annotations, http://schrealex.com/downloads/User.java:

@Entity
@Table(name="user")
public class User {

    @Id
    @Column(name="user_id", unique=true, nullable=false)
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    @Column(name="username")
    private String username;

    @Column(name="password")
    private String password;

    @Column(name="firstname")
    private String firstname;

    @Column(name="lastname")
    private String lastname;

    @Column(name="birthDate")
    private Date birthDate;

    @Column(name="email")
    private String email;

    @Column(name="profile_image")
    private String profile_image;

    public User() {

    }

    public User(String username, String password, String email) {
        this.username = username;
        this.password = password;
        this.email = email;

    }

    // Getter and Setter methods

}

我正在使用pom.xml中描述的以下依赖项:

I'm using the following dependencies described in my pom.xml:

http://schrealex.com/downloads/pom.xml

我在applicationContext.xml和属性中使用以下配置:

I'm using the following configuration in applicationContext.xml and properties:

http://schrealex.com/downloads/application.properties

http://schrealex.com/downloads/applicationContext.xml

最后是web.xml:

And finally web.xml:

http://schrealex.com/downloads/web.xml

如果我想丢失任何文件,请问.

If I'm missing any files you'd like to see, just ask.

-

添加了启动日志记录:

SSL access to the quickstart has been enabled on port 8443
You can access the application using SSL on https://localhost:8443

>>> STARTING EMBEDDED JETTY SERVER, PRESS ANY KEY TO STOP
INFO  - Server                     - jetty-7.6.3.v20120416
INFO  - tandardDescriptorProcessor - NO JSP Support for /, did not find org.apache.jasper.servlet.JspServlet
INFO  - /                          - Initializing Spring root WebApplicationContext
INFO  - ContextLoader              - Root WebApplicationContext: initialization started
INFO  - XmlWebApplicationContext   - Refreshing org.springframework.web.context.support.XmlWebApplicationContext@1c35ce99: display name [Root WebApplicationContext]; startup date [Wed Nov 28 19:53:33 CET 2012]; root of context hierarchy
INFO  - XmlBeanDefinitionReader    - Loading XML bean definitions from class path resource [applicationContext.xml]
INFO  - XmlWebApplicationContext   - Bean factory for application context [org.springframework.web.context.support.XmlWebApplicationContext@1c35ce99]: org.springframework.beans.factory.support.DefaultListableBeanFactory@2a9b5441
INFO  - pertyPlaceholderConfigurer - Loading properties file from URL [file:/C:/Users/CE_REAL/Documents/Development/media-database/target/classes/application.properties]
INFO  - DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2a9b5441: defining beans [wicketApplication,placeholderConfigurer,dataSource,transactionManager,transactionInterceptor,managerTemplate,sessionFactory]; root of factory hierarchy
INFO  - Version                    - Hibernate Annotations 3.4.0.GA
INFO  - Environment                - Hibernate 3.2.6
INFO  - Environment                - hibernate.properties not found
INFO  - Environment                - Bytecode provider name : cglib
INFO  - Environment                - using JDK 1.4 java.sql.Timestamp handling
INFO  - Version                    - Hibernate Commons Annotations 3.1.0.GA
INFO  - AnnotationConfiguration    - Hibernate Validator not found: ignoring
INFO  - notationSessionFactoryBean - Building new Hibernate SessionFactory
INFO  - earchEventListenerRegister - Unable to find org.hibernate.search.event.FullTextIndexEventListener on the classpath. Hibernate Search is not enabled.
INFO  - ConnectionProviderFactory  - Initializing connection provider: org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider
INFO  - SettingsFactory            - RDBMS: MySQL, version: 5.5.16-log
INFO  - SettingsFactory            - JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.1.21 ( Revision: ${bzr.revision-id} )
INFO  - Dialect                    - Using dialect: org.hibernate.dialect.MySQLDialect
INFO  - TransactionFactoryFactory  - Transaction strategy: org.springframework.orm.hibernate3.SpringTransactionFactory
INFO  - actionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
INFO  - SettingsFactory            - Automatic flush during beforeCompletion(): disabled
INFO  - SettingsFactory            - Automatic session close at end of transaction: disabled
INFO  - SettingsFactory            - JDBC batch size: 15
INFO  - SettingsFactory            - JDBC batch updates for versioned data: disabled
INFO  - SettingsFactory            - Scrollable result sets: enabled
INFO  - SettingsFactory            - JDBC3 getGeneratedKeys(): enabled
INFO  - SettingsFactory            - Connection release mode: auto
INFO  - SettingsFactory            - Maximum outer join fetch depth: 2
INFO  - SettingsFactory            - Default batch fetch size: 1
INFO  - SettingsFactory            - Generate SQL with comments: disabled
INFO  - SettingsFactory            - Order SQL updates by primary key: disabled
INFO  - SettingsFactory            - Order SQL inserts for batching: disabled
INFO  - SettingsFactory            - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
INFO  - ASTQueryTranslatorFactory  - Using ASTQueryTranslatorFactory
INFO  - SettingsFactory            - Query language substitutions: {}
INFO  - SettingsFactory            - JPA-QL strict compliance: disabled
INFO  - SettingsFactory            - Second-level cache: enabled
INFO  - SettingsFactory            - Query cache: disabled
INFO  - SettingsFactory            - Cache provider: org.hibernate.cache.EhCacheProvider
INFO  - SettingsFactory            - Optimize cache for minimal puts: disabled
INFO  - SettingsFactory            - Structured second-level cache entries: disabled
INFO  - SettingsFactory            - Echoing all SQL to stdout
INFO  - SettingsFactory            - Statistics: disabled
INFO  - SettingsFactory            - Deleted entity synthetic identifier rollback: disabled
INFO  - SettingsFactory            - Default entity-mode: pojo
INFO  - SettingsFactory            - Named query checking : enabled
INFO  - SessionFactoryImpl         - building session factory
WARN  - ConfigurationFactory       - No configuration found. Configuring ehcache from ehcache-failsafe.xml  found in the classpath: jar:file:/C:/Users/CE_REAL/.m2/repository/net/sf/ehcache/ehcache/1.2.3/ehcache-1.2.3.jar!/ehcache-failsafe.xml
INFO  - essionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
INFO  - SchemaExport               - Running hbm2ddl schema export
INFO  - SchemaExport               - exporting generated schema to database
INFO  - SchemaExport               - schema export complete
INFO  - ContextLoader              - Root WebApplicationContext: initialization completed in 671 ms
INFO  - ContextHandler             - started o.e.j.w.WebAppContext{/,file:/C:/Users/CE_REAL/Documents/Development/media-database/src/main/webapp/},src/main/webapp
WARN  - WebXmlFile                 - web.xml: No url-pattern found for 'filter' with name 'wicket-spring-hibernate'
INFO  - WebXmlFile                 - web.xml: url mapping found for filter with name wicket-spring-hibernate:
WARN  - WicketFilter               - Unable to determine filter path from filter init-param, web.xml, or servlet 3.0 annotations. Assuming user will set filter path manually by calling setFilterPath(String)
INFO  - Application                - [wicket-spring-hibernate] init: Wicket core library initializer
INFO  - RequestListenerInterface   - registered listener interface [RequestListenerInterface name=IBehaviorListener, method=public abstract void org.apache.wicket.behavior.IBehaviorListener.onRequest()]
INFO  - RequestListenerInterface   - registered listener interface [RequestListenerInterface name=IFormSubmitListener, method=public abstract void org.apache.wicket.markup.html.form.IFormSubmitListener.onFormSubmitted()]
INFO  - RequestListenerInterface   - registered listener interface [RequestListenerInterface name=ILinkListener, method=public abstract void org.apache.wicket.markup.html.link.ILinkListener.onLinkClicked()]
INFO  - RequestListenerInterface   - registered listener interface [RequestListenerInterface name=IOnChangeListener, method=public abstract void org.apache.wicket.markup.html.form.IOnChangeListener.onSelectionChanged()]
INFO  - RequestListenerInterface   - registered listener interface [RequestListenerInterface name=IRedirectListener, method=public abstract void org.apache.wicket.IRedirectListener.onRedirect()]
INFO  - RequestListenerInterface   - registered listener interface [RequestListenerInterface name=IResourceListener, method=public abstract void org.apache.wicket.IResourceListener.onResourceRequested()]
INFO  - Application                - [wicket-spring-hibernate] init: Wicket extensions initializer
INFO  - WebApplication             - [wicket-spring-hibernate] Started Wicket version 6.2.0 in DEVELOPMENT mode
********************************************************************
*** WARNING: Wicket is running in DEVELOPMENT mode.              ***
***                               ^^^^^^^^^^^                    ***
*** Do NOT deploy to your live server(s) without changing this.  ***
*** See Application#getConfigurationType() for more information. ***
********************************************************************
INFO  - WebXmlFile                 - web.xml: url mapping found for filter with name wicket.media-database: [/login/*]
INFO  - Application                - [wicket.media-database] init: Wicket core library initializer
INFO  - Application                - [wicket.media-database] init: Wicket extensions initializer
INFO  - WebApplication             - [wicket.media-database] Started Wicket version 6.2.0 in DEVELOPMENT mode
********************************************************************
*** WARNING: Wicket is running in DEVELOPMENT mode.              ***
***                               ^^^^^^^^^^^                    ***
*** Do NOT deploy to your live server(s) without changing this.  ***
*** See Application#getConfigurationType() for more information. ***
********************************************************************
INFO  - AbstractConnector          - Started SocketConnector@0.0.0.0:8080
INFO  - SslContextFactory          - Enabled Protocols [SSLv2Hello, SSLv3, TLSv1, TLSv1.1, TLSv1.2] of [SSLv2Hello, SSLv3, TLSv1, TLSv1.1, TLSv1.2]
INFO  - AbstractConnector          - Started SslSocketConnector@0.0.0.0:8443

-

我尝试将User类重命名为MediaUser,并将@Table批注重命名为mediaUser,以避免为某些数据库保留USER字的问题.

I tried renaming my User class to MediaUser and the @Table annotation to mediaUser to avoid problems with the USER word being reserved for some databases.

我从启动日志中发现,如上所述,它确实表明它正在运行导出:

What I've found from the start up logging, as seen above, is that it does say it's running the export:

INFO - SchemaExport - Running hbm2ddl schema export 
INFO - SchemaExport - exporting generated schema to database 
INFO - SchemaExport - schema export complete

还尝试了使用Hibernate批注的其他方法,例如使用其他导入,@ Table现在用于导入javax.persistence.Table,但我也尝试了Hibernate org.hibernate.annotations.table,到目前为止,还没有解决方案我的问题.

Also tried different approaches to the Hibernate annotations, like using other imports, @Table is now used by importing javax.persistence.Table, but I also tried the Hibernate org.hibernate.annotations.table, thus far without a solution to my problem.

推荐答案

我已经找到问题的答案,注释需要在实例变量或类及其方法上设置,并且需要实现Serializable像:

I've found the answer to my problem, the annotations need either be set on the instance variables or on the class and it's methods and it needs to implement Serializable like:

@Entity
public class User implements Serializable {

    @Id
    @GeneratedValue
    private Long id;

    private String username;
    private String password;
    private String firstname;
    private String lastname;
    private Date birthDate;
    private String email;
    private String profileImage;

    public User() {

    }

    public User(String username, String password, String email) {
        this.username = username;
        this.password = password;
        this.email = email;

    }

    // Getter and Setter methods

    @Column
    public getUsername() {
        return username;
    }

    @Column
    public getPassword() {
        return password;
    }

    @Column
    public getFirstname() {
        return firstname;
    }

    @Column
    public getLastname() {
        return lastname;
    }

    @Column
    @Temporal(TemporalType.TIME)
    public getBirthDate() {
        return birthDate;
    }

    @Column
    public getEmail() {
        return email;
    }

    @Column
    public getProfileImage() {
        return profileImage;
    }
}

这篇关于Hibernate不会生成带有注释的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆