鱼C论坛

 找回密码
 立即注册
查看: 4039|回复: 14

[已解决]Myeclipse下建立Hibernate框架不能在mysql建立表

[复制链接]
发表于 2018-1-14 23:51:21 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
测试类
  1. package club.mochunrong.hibernate.test;

  2. import org.hibernate.Session;
  3. import org.hibernate.SessionFactory;
  4. import org.hibernate.Transaction;
  5. import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
  6. import org.hibernate.cfg.Configuration;
  7. import org.hibernate.service.ServiceRegistry;

  8. import club.mochunrong.hibernate.bean.User;

  9. public class HibernateTest {

  10.         public static void main(String[] args)
  11.           {
  12.             try
  13.             {
  14.               Configuration configuration = new Configuration().configure();
  15.              
  16.               System.out.println(configuration);
  17.               ServiceRegistry serviceRegistry =
  18.                 new StandardServiceRegistryBuilder().applySettings(configuration.getProperties())
  19.                 .build();
  20.               System.out.println(serviceRegistry);
  21.               SessionFactory sf = configuration.buildSessionFactory(serviceRegistry);   
  22.               System.out.println(sf);
  23.              

  24.             
  25.             }
  26.             catch (Exception e)
  27.             {
  28.                      e.printStackTrace();
  29.             }
  30.           }

  31. }

复制代码



配置文件
  1. <!DOCTYPE hibernate-configuration PUBLIC
  2.         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  3.         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  4. <hibernate-configuration>
  5.         <session-factory>
  6.                 <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
  7.                 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  8.                 <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/haha</property>
  9.                 <property name="hibernate.connection.username">root</property>
  10.                 <property name="hibernate.connection.password">chunrong</property>
  11.                 <property name="hibernate.c3p0.max_size">10</property>
  12.                 <property name="hibernate.c3p0.min_size">2</property>
  13.                 <property name="hibernate.c3p0.timeout">5000</property>
  14.                 <property name="hibernate.show_sql">true</property>
  15.                 <property name="hibernate.format_sql">true</property>
  16.                 <property name="hibernate.hbm2ddl.auto">update</property>
  17.                 <mapping class="club.mochunrong.hibernate.bean.User" />


  18.         </session-factory>
  19. </hibernate-configuration>
复制代码



持久化类

  1. package club.mochunrong.hibernate.bean;

  2. import java.io.Serializable;

  3. import javax.persistence.Column;
  4. import javax.persistence.Entity;
  5. import javax.persistence.Id;
  6. @Entity(name="tb_user")
  7. public class User implements Serializable
  8. {
  9.   @Id
  10.   @Column(name="TB_ID")
  11.   private int id;
  12.   @Column(name="TB_NAME")
  13.   private String name;
  14.   private int age;
  15.   
  16.   public int getId()
  17.   {
  18.     return this.id;
  19.   }
  20.   
  21.   public void setId(int id)
  22.   {
  23.     this.id = id;
  24.   }
  25.   
  26.   public String getName()
  27.   {
  28.     return this.name;
  29.   }
  30.   
  31.   public void setName(String name)
  32.   {
  33.     this.name = name;
  34.   }
  35.   
  36.   public int getAge()
  37.   {
  38.     return this.age;
  39.   }
  40.   
  41.   public void setAge(int age)
  42.   {
  43.     this.age = age;
  44.   }
  45.   
  46.   public String toString()
  47.   {
  48.     return "User [id=" + this.id + ", name=" + this.name + ", age=" + this.age + "]";
  49.   }
  50. }
复制代码



===========================================================================
                                                console的打印日志
===========================================================================
一月 14, 2018 11:37:00 下午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.12.Final}
一月 14, 2018 11:37:00 下午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000205: Loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=org.h2.Driver, hibernate.service.allow_crawling=false, hibernate.max_fetch_depth=5, hibernate.dialect=org.hibernate.dialect.H2Dialect, hibernate.format_sql=true, hibernate.generate_statistics=true, hibernate.connection.username=sa, hibernate.connection.url=jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;LOCK_TIMEOUT=10000, hibernate.bytecode.use_reflection_optimizer=false, hibernate.connection.password=****, hibernate.connection.pool_size=5}
org.hibernate.cfg.Configuration@1d422d
一月 14, 2018 11:37:00 下午 org.hibernate.spatial.integration.SpatialService <init>
INFO: HHH80000001: hibernate-spatial integration enabled : true
org.hibernate.boot.registry.internal.StandardServiceRegistryImpl@1d4520b
一月 14, 2018 11:37:00 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
一月 14, 2018 11:37:00 下午 org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
INFO: HHH010002: C3P0 using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://127.0.0.1:3306/haha
一月 14, 2018 11:37:00 下午 org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
INFO: HHH10001001: Connection properties: {user=root, password=****}
一月 14, 2018 11:37:00 下午 org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
INFO: HHH10001003: Autocommit mode: false
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
MLog initialization issue: slf4j found no binding or threatened to use its (dangerously silent) NOPLogger. We consider the slf4j library not found.
一月 14, 2018 11:37:00 下午 com.mchange.v2.log.MLog
信息: MLog clients using java 1.4+ standard logging.
一月 14, 2018 11:37:01 下午 com.mchange.v2.c3p0.C3P0Registry
信息: Initializing c3p0-0.9.5.2 [built 08-December-2015 22:06:04 -0800; debug? true; trace: 10]
一月 14, 2018 11:37:01 下午 org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
INFO: HHH10001007: JDBC isolation level: <unknown>
一月 14, 2018 11:37:01 下午 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource
信息: Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@eb25adb5 [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@32f3b794 [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, contextClassLoaderSource -> caller, debugUnreturnedConnectionStackTraces -> false, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, forceSynchronousCheckins -> false, identityToken -> 1hgery99s1w2efcj18ld78h|1835c46, idleConnectionTestPeriod -> 0, initialPoolSize -> 2, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 5000, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 10, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 2, nestedDataSource -> com.mchange.v2.c3p0.DriverManagerDataSource@edb4e294 [ description -> null, driverClass -> null, factoryClassLocation -> null, forceUseNamedDriverClass -> false, identityToken -> 1hgery99s1w2efcj18ld78h|386f16, jdbcUrl -> jdbc:mysql://127.0.0.1:3306/haha, properties -> {user=******, password=******} ], preferredTestQuery -> null, privilegeSpawnedThreads -> false, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false; userOverrides: {} ], dataSourceName -> null, extensions -> {}, factoryClassLocation -> null, identityToken -> 1hgery99s1w2efcj18ld78h|3ef382, numHelperThreads -> 3 ]
一月 14, 2018 11:37:01 下午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLInnoDBDialect
一月 14, 2018 11:37:01 下午 org.hibernate.envers.boot.internal.EnversServiceImpl configure
INFO: Envers integration enabled? : true
一月 14, 2018 11:37:01 下午 org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@1eef88a] 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.
org.hibernate.internal.SessionFactoryImpl@16c53fc



===========================================================================


导包问题,我把hibernate的所有包都导进去了,mysql驱动包也导了,


数据库能正常运行使用。







请问各位有经验的大佬是否遇到这样的事情,或者知道怎么解决的告诉我一声谢谢。


                                                        我设置了回答有奖励
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-1-16 09:54:40 | 显示全部楼层

回帖奖励 +2 鱼币

配置文件里没有引入映射文件
<mapping class="club.mochunrong.hibernate.bean.User" />     
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-1-16 15:56:39 | 显示全部楼层
夜色下的俯瞰 发表于 2018-1-16 09:54
配置文件里没有引入映射文件

我用注解的,不是属性文件

@Entity(name="tb_user")

持久化类里面上有的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-17 09:05:47 | 显示全部楼层
chun888 发表于 2018-1-16 15:56
我用注解的,不是属性文件

@Entity(name="tb_user")

用注解也要配置啊,我之前说的不清楚,你要在配置文件中注册的注解了的实体类
<mapping class="club.mochunrong.hibernate.bean.User" />     
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-17 09:37:17 | 显示全部楼层

回帖奖励 +2 鱼币

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-3-12 16:28:32 | 显示全部楼层

回帖奖励 +2 鱼币

我也碰到差不多的问题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-4-26 11:11:45 | 显示全部楼层
段坤11 发表于 2018-3-12 16:28
我也碰到差不多的问题

其实问题早已自己解决了,想知道该论坛能否有人给出我满意答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-12 14:33:19 | 显示全部楼层    本楼为最佳答案   

回帖奖励 +2 鱼币

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-7-22 20:40:09 | 显示全部楼层

回帖奖励 +2 鱼币

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-8-8 21:24:54 | 显示全部楼层

回帖奖励 +2 鱼币

路过学习哈
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-15 12:17:41 | 显示全部楼层

回帖奖励 +2 鱼币

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-9-7 21:42:33 | 显示全部楼层

回帖奖励 +2 鱼币

加油
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-9-16 11:56:26 | 显示全部楼层
宝宝
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-9-16 11:57:00 | 显示全部楼层
&#128552;
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-16 17:24:47 | 显示全部楼层

回帖奖励 +2 鱼币

...
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-24 10:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表