【问题标题】:Hibernate without xml没有xml的休眠
【发布时间】:2014-05-25 10:16:13
【问题描述】:

我正在尝试使用 hibernate 连接到 mysql 数据库。首先,我想摆脱 hibernate.cfg.xml 并改用 hibernate.properties。

hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url= jdbc:mysql://localhost:3306/dealer
hibernate.connection.username=root
hibernate.connection.password=root
hibernate.connection.pool_size=1
hibernate.transaction.factory_class = org.hibernate.transaction.JTATransactionFactory
hibernate.transaction.manager_lookup_class = org.hibernate.transaction.JBossTransactionManagerLookup
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.packagesToScan = domain
hibernate.autocommit = true

这就是我的 hibernate.properties 目前的样子。 我还为 sessionfactory 创建了这个类:

public class HibernateUtils {
    private static SessionFactory sessionFactory;
    private static ServiceRegistry serviceRegistry;

    static {
        try {
            Logger.getLogger("org.hibernate").setLevel(Level.ALL);
            Configuration configuration = new Configuration();
            serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        } catch (HibernateException exception) {
            System.out.println("Problem creating session factory");
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

现在,我的问题是我不知道它是如何工作的,也没有找到关于如何做到这一点的好教程。我知道连接很好,但我用 @Entity 注释的类不是在数据库中创建的。谁能告诉我这是否可以在不使用 hibernate.cfg.xml 的情况下完成,如果可以,接下来我该怎么做?

L.E. 这是日志

Mai 25, 2014 1:19:56 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
Mai 25, 2014 1:19:56 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.2.Final}
Mai 25, 2014 1:19:56 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000205: Loaded properties from resource hibernate.properties: {hibernate.packagesToScan=domain, hibernate.connection.driver_class=com.mysql.jdbc.Driver, hibernate.transaction.manager_lookup_class=org.hibernate.transaction.JBossTransactionManagerLookup, hibernate.autocommit=true, hibernate.dialect=org.hibernate.dialect.MySQLDialect, hibernate.connection.username=root, hibernate.connection.url=jdbc:mysql://localhost:3306/dealer, hibernate.bytecode.use_reflection_optimizer=false, hibernate.connection.password=****, hibernate.transaction.factory_class=org.hibernate.transaction.JTATransactionFactory, hibernate.connection.pool_size=1}
Mai 25, 2014 1:19:56 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Mai 25, 2014 1:19:56 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Mai 25, 2014 1:19:56 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 1
Mai 25, 2014 1:19:56 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: false
Mai 25, 2014 1:19:56 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/dealer]
Mai 25, 2014 1:19:56 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=root, password=root}
Mai 25, 2014 1:19:56 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Mai 25, 2014 1:19:56 PM org.hibernate.service.jta.platform.internal.JtaPlatformInitiator getConfiguredPlatform
WARN: HHH000427: Using deprecated org.hibernate.transaction.TransactionManagerLookup strategy [hibernate.transaction.manager_lookup_class], use newer org.hibernate.service.jta.platform.spi.JtaPlatform strategy instead [hibernate.transaction.jta.platform]
Mai 25, 2014 1:19:56 PM org.hibernate.service.jta.platform.internal.JtaPlatformInitiator mapLegacyClasses
INFO: HHH000428: Encountered legacy TransactionManagerLookup specified; convert to newer org.hibernate.service.jta.platform.spi.JtaPlatform contract specified via hibernate.transaction.jta.platform setting
Mai 25, 2014 1:19:56 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000268: Transaction strategy: org.hibernate.engine.transaction.internal.jta.JtaTransactionFactory
Mai 25, 2014 1:19:56 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory

【问题讨论】:

标签: java mysql xml hibernate


【解决方案1】:

您需要将所有带实体注释的类添加到会话工厂,以将它们插入数据库:

sessionFactory.addPackage("com.concretepage.persistence")
    .addProperties(prop).addAnnotatedClass(User.class).buildSessionFactory();

【讨论】:

    【解决方案2】:

    将以下属性添加到 hibernate.properties

    hibernate.hbm2ddl.auto = update
    

    【讨论】:

      猜你喜欢
      • 2012-01-25
      • 2015-06-23
      • 1970-01-01
      • 2016-05-06
      • 2010-12-26
      • 2017-07-09
      • 1970-01-01
      • 1970-01-01
      • 2021-08-28
      相关资源
      最近更新 更多