有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

无xml的java Hibernate

我正在尝试使用hibernate连接到mysql数据库。首先,我想摆脱冬眠。cfg。xml并使用hibernate。取而代之的是财产

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

这就是我冬眠的方式。房地产目前看起来很不错。 我还为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

共 (2) 个答案

  1. # 1 楼答案

    将以下属性添加到休眠。属性

    hibernate.hbm2ddl.auto = update
    
  2. # 2 楼答案

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

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