论坛首页 Java企业应用论坛

hibernate入门篇之新增功能!

浏览 42352 次
该帖已经被评为精华帖
作者 正文
   发表时间:2004-02-13  
不好意思,xml文件贴错了,补上
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping >
    <class name="com.hjtx.test.Message" table="Message" >
    <id name = "id" column = "MESSAGE_ID" >
    <generator class="identity" />
    </id>
    <property name="text" type="string" >
    <column name="TEXT" length="100" not-null="true"/>
    </property>
    <property name="nextMessage" column="NEXT_MESSAGE" type="string" length="100" />
    </class>

</hibernate-mapping>
0 请登录后投票
   发表时间:2004-03-24  
我按照上面的例子,用eclipes做了一下试验,但是出现了问题,整个提示信息为:
hibernate.cfg.xml
<?xml version='1.0' encoding='big5'?>
<!DOCTYPE hibernate-configuration
    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
  <session-factory>
  <!-- session factory properties -->   
  <!--property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property-->
  <property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
  <property name="hibernate.connection.url">jdbc:mysql://172.19.25.43:3306/poweracl</property>   
  <property name="hibernate.connection.username">root</property>   
  <property name="hibernate.connection.password"></property>
  <property name="hibernate.connection.pool.size">20</property>
  <property name="hibernate.show_sql">true</property>
  <!--property name="jdbc.fetch_size">50</property-->
  <!--property name="jdbc.batch_size">25</property-->
  <!--property name="jdbc.use_scrollable_resultset">false</property-->
  <property name="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</property>
  <!--property name="hibernate.cache.provider_class">EHCache</property-->

  <!-- Mapping object 的對應檔案 -->   
  <mapping resource="Person.hbm.xml"/>   
  </session-factory>
</hibernate-configuration>



import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.tool.hbm2ddl.SchemaExport;

import java.io.File;

class TestPerson
{
private static SessionFactory sessions;

public static void main(String[] args) throws Exception{
//配置環境,分析xml映射文件
Configuration conf= new Configuration().configure(new File("hibernate.cfg.xml"));
//生成並輸出sql到文件(當前目錄)和資料庫
SchemaExport dbExport=new SchemaExport(conf);
dbExport.setOutputFile("sql.txt");
dbExport.create(true, true);
sessions = conf.buildSessionFactory();
//以上都是些固定格式的環境配置
//start......
Session s = HibernateUtil.currentSession();
Transaction t= s.beginTransaction();
//Session s = sessions.openSession();
//Transaction t = s.beginTransaction();   //1.用普通使用方式建立物件,填充資料
Person p1=new Person();
p1.setEmail("smallduzi@sohu.com");
p1.setName("陳鵬");
p1.setAddress("西安東郊黃陵");
//2.持久化
s.save(p1);
//此時p1已經可以在資料庫中找到
t.commit();
//s.close();
HibernateUtil.closeSession();
}
}

2004/3/24 上午 10:44:47 net.sf.hibernate.cfg.Environment <clinit>
資訊: Hibernate 2.1.2
2004/3/24 上午 10:44:47 net.sf.hibernate.cfg.Environment <clinit>
資訊: loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=org.gjt.mm.mysql.Driver, hibernate.cglib.use_reflection_optimizer=true, hibernate.dialect=net.sf.hibernate.dialect.MySQLDialect, hhibernate.connection.username=root, hibernate.connection.url=jdbc:mysql://172.19.25.43:3306/poweracl?useUnicode=true&characterEncoding=big5, hibernate.show_sql=true, hibernate.connection.password=, hibernate.connection.pool_size=20, hibernate.statement_cache.size=6}
2004/3/24 上午 10:44:47 net.sf.hibernate.cfg.Environment <clinit>
資訊: using CGLIB reflection optimizer
2004/3/24 上午 10:44:47 net.sf.hibernate.cfg.Configuration configure
資訊: configuring from file: hibernate.cfg.xml
2004/3/24 上午 10:44:47 net.sf.hibernate.cfg.Configuration addResource
資訊: Mapping resource: Person.hbm.xml
2004/3/24 上午 10:44:47 net.sf.hibernate.cfg.Binder bindRootClass
資訊: Mapping class: Person -> person
2004/3/24 上午 10:44:47 net.sf.hibernate.cfg.Configuration doConfigure
資訊: Configured SessionFactory: null
2004/3/24 上午 10:44:48 net.sf.hibernate.dialect.Dialect <init>
資訊: Using dialect: net.sf.hibernate.dialect.MySQLDialect
2004/3/24 上午 10:44:48 net.sf.hibernate.cfg.Configuration secondPassCompile
資訊: processing one-to-many association mappings
2004/3/24 上午 10:44:48 net.sf.hibernate.cfg.Configuration secondPassCompile
資訊: processing one-to-one association property references
2004/3/24 上午 10:44:48 net.sf.hibernate.cfg.Configuration secondPassCompile
資訊: processing foreign key constraints
2004/3/24 上午 10:44:48 net.sf.hibernate.cfg.Configuration secondPassCompile
資訊: processing one-to-many association mappings
2004/3/24 上午 10:44:48 net.sf.hibernate.cfg.Configuration secondPassCompile
資訊: processing one-to-one association property references
2004/3/24 上午 10:44:48 net.sf.hibernate.cfg.Configuration secondPassCompile
資訊: processing foreign key constraints
2004/3/24 上午 10:44:48 net.sf.hibernate.tool.hbm2ddl.SchemaExport execute
資訊: Running hbm2ddl schema export
2004/3/24 上午 10:44:48 net.sf.hibernate.tool.hbm2ddl.SchemaExport execute
資訊: writing generated schema to file: sql.txt
2004/3/24 上午 10:44:48 net.sf.hibernate.tool.hbm2ddl.SchemaExport execute
資訊: exporting generated schema to database
2004/3/24 上午 10:44:48 net.sf.hibernate.connection.DriverManagerConnectionProvider configure
資訊: Using Hibernate built-in connection pool (not for production use!)
2004/3/24 上午 10:44:48 net.sf.hibernate.connection.DriverManagerConnectionProvider configure
資訊: Hibernate connection pool size: 20
2004/3/24 上午 10:44:48 net.sf.hibernate.connection.DriverManagerConnectionProvider configure
資訊: using driver: org.gjt.mm.mysql.Driver at URL: jdbc:mysql://172.19.25.43:3306/poweracl
2004/3/24 上午 10:44:48 net.sf.hibernate.connection.DriverManagerConnectionProvider configure
資訊: connection properties: {user=root, .username=root, password=, pool.size=20}
drop table if exists person
create table person (
   id VARCHAR(40) NOT NULL AUTO_INCREMENT,
   name VARCHAR(255),
   email VARCHAR(255),
   address VARCHAR(255),
   primary key (id)
)
2004/3/24 上午 10:44:48 net.sf.hibernate.tool.hbm2ddl.SchemaExport execute
嚴重的: Unsuccessful: create table person (id VARCHAR(40) NOT NULL AUTO_INCREMENT, name VARCHAR(255), email VARCHAR(255), address VARCHAR(255), primary key (id))
2004/3/24 上午 10:44:48 net.sf.hibernate.tool.hbm2ddl.SchemaExport execute
嚴重的: Invalid argument value,  message from server: "Incorrect column specifier for column 'id'"
2004/3/24 上午 10:44:48 net.sf.hibernate.tool.hbm2ddl.SchemaExport execute
資訊: schema export complete
2004/3/24 上午 10:44:48 net.sf.hibernate.connection.DriverManagerConnectionProvider close
資訊: cleaning up connection pool: jdbc:mysql://172.19.25.43:3306/poweracl
2004/3/24 上午 10:44:48 net.sf.hibernate.cfg.Configuration secondPassCompile
資訊: processing one-to-many association mappings
2004/3/24 上午 10:44:48 net.sf.hibernate.cfg.Configuration secondPassCompile
資訊: processing one-to-one association property references
2004/3/24 上午 10:44:48 net.sf.hibernate.cfg.Configuration secondPassCompile
資訊: processing foreign key constraints
2004/3/24 上午 10:44:48 net.sf.hibernate.dialect.Dialect <init>
資訊: Using dialect: net.sf.hibernate.dialect.MySQLDialect
2004/3/24 上午 10:44:48 net.sf.hibernate.cfg.SettingsFactory buildSettings
資訊: Use outer join fetching: true
2004/3/24 上午 10:44:48 net.sf.hibernate.connection.DriverManagerConnectionProvider configure
資訊: Using Hibernate built-in connection pool (not for production use!)
2004/3/24 上午 10:44:48 net.sf.hibernate.connection.DriverManagerConnectionProvider configure
資訊: Hibernate connection pool size: 20
2004/3/24 上午 10:44:48 net.sf.hibernate.connection.DriverManagerConnectionProvider configure
資訊: using driver: org.gjt.mm.mysql.Driver at URL: jdbc:mysql://172.19.25.43:3306/poweracl
2004/3/24 上午 10:44:48 net.sf.hibernate.connection.DriverManagerConnectionProvider configure
資訊: connection properties: {user=root, .username=root, password=, pool.size=20}
2004/3/24 上午 10:44:48 net.sf.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
資訊: No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)
2004/3/24 上午 10:44:48 net.sf.hibernate.cfg.SettingsFactory buildSettings
資訊: Use scrollable result sets: true
2004/3/24 上午 10:44:48 net.sf.hibernate.cfg.SettingsFactory buildSettings
資訊: Use JDBC3 getGeneratedKeys(): true
2004/3/24 上午 10:44:48 net.sf.hibernate.cfg.SettingsFactory buildSettings
資訊: Optimize cache for minimal puts: false
2004/3/24 上午 10:44:48 net.sf.hibernate.cfg.SettingsFactory buildSettings
資訊: echoing all SQL to stdout
2004/3/24 上午 10:44:48 net.sf.hibernate.cfg.SettingsFactory buildSettings
資訊: Query language substitutions: {}
2004/3/24 上午 10:44:48 net.sf.hibernate.cfg.SettingsFactory buildSettings
資訊: cache provider: net.sf.ehcache.hibernate.Provider
net.sf.hibernate.HibernateException: could not instantiate CacheProvider: net.sf.ehcache.hibernate.Provider
at net.sf.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:133)
at net.sf.hibernate.cfg.Configuration.buildSettings(Configuration.java:1119)
at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:748)
at TestPerson.main(TestPerson.java:22)
Caused by: java.lang.ClassNotFoundException: net.sf.ehcache.hibernate.Provider
at java.net.URLClassLoader$1.run(URLClassLoader.java:198)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:186)
at java.lang.ClassLoader.loadClass(ClassLoader.java:299)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:265)
at java.lang.ClassLoader.loadClass(ClassLoader.java:255)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:315)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:140)
at net.sf.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:86)
at net.sf.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:130)
... 3 more
Exception in thread "main"

請問wrong where ?
0 请登录后投票
   发表时间:2004-03-24  
解決一個又出一個問題??
請問一下... run  出現問題 ,
我用 eclipse 3.0 環境
什麼 JNDI 設定在 ?地方?

資訊: building session factory
2004/3/24 下午 05:04:31 net.sf.hibernate.impl.SessionFactoryObjectFactory addInstance
資訊: no JNDI name configured
java.lang.NoClassDefFoundError: javax/transaction/Synchronization
  at net.sf.hibernate.impl.SessionFactoryImpl.openSession(SessionFactoryImpl.java:312)
  at net.sf.hibernate.impl.SessionFactoryImpl.openSession(SessionFactoryImpl.java:325)
  at net.sf.hibernate.impl.SessionFactoryImpl.openSession(SessionFactoryImpl.java:333)
  at TestPerson.main(TestPerson.java:27)
Exception in thread "main"
0 请登录后投票
   发表时间:2004-03-27  
信息: no JNDI name configured
java.lang.NoClassDefFoundError: javax/transaction/Synchronization
at net.sf.hibernate.impl.SessionFactoryImpl.openSession(SessionFactoryImpl.java:312)
at net.sf.hibernate.impl.SessionFactoryImpl.openSession(SessionFactoryImpl.java:325)
at net.sf.hibernate.impl.SessionFactoryImpl.openSession(SessionFactoryImpl.java:333)
at hiberante.Example.main(Example.java:50)
Exception in thread "main"

是为什么?
0 请登录后投票
   发表时间:2004-05-03  
倒..............
我运行时出现的错误是:
java.lang.NoClassDefFoundError: org/dom4j/Attribute
at com.javamodel.hibernate.Example.<clinit>(Example.java:39)
Exception in thread "main"
0 请登录后投票
   发表时间:2004-05-05  
答应过smallduzi兄对他的例子Sqlserver版的细致说明
到现在才整理实是不好意思

写在前面的话:
回看出现过的错误,一般都是缺库文件所导致。附件图是我试出来的最少必需库清单。再不行可以把所有的库都加上。另msSQLJDBCDriver.jar是我把MS提供的三个JAR文件,重新打包成一个JAR。如果不用TangHan插件,可以就加入原来的三个JAR文件。

首先:代码当然是按原来的建立了。

  2 :
加入所需要的库文件,见下图:

  3 :

hibernate.properties内容改为(数据库、用户和密码请改为你自己的):
## SQLServer

hibernate.dialect net.sf.hibernate.dialect.SQLServerDialect
hibernate.connection.driver_class com.microsoft.jdbc.sqlserver.SQLServerDriver
hibernate.connection.username sa
hibernate.connection.password 123
hibernate.connection.url jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=blueRain


    4:运行后的信息提示:
2004-5-5 11:49:31 net.sf.hibernate.cfg.Environment <clinit>
信息: Hibernate 2.1.2
2004-5-5 11:49:32 net.sf.hibernate.cfg.Environment <clinit>
信息: hibernate.properties not found
2004-5-5 11:49:32 net.sf.hibernate.cfg.Environment <clinit>
信息: using CGLIB reflection optimizer
2004-5-5 11:49:32 net.sf.hibernate.cfg.Configuration addClass
信息: Mapping resource: com/javamodel/hibernate/Person.hbm.xml
2004-5-5 11:49:33 net.sf.hibernate.cfg.Binder bindRootClass
信息: Mapping class: com.javamodel.hibernate.Person -> person
2004-5-5 11:49:33 net.sf.hibernate.cfg.Configuration secondPassCompile
信息: processing one-to-many association mappings
2004-5-5 11:49:33 net.sf.hibernate.cfg.Configuration secondPassCompile
信息: processing one-to-one association property references
2004-5-5 11:49:33 net.sf.hibernate.cfg.Configuration secondPassCompile
信息: processing foreign key constraints
2004-5-5 11:49:33 net.sf.hibernate.dialect.Dialect <init>
信息: Using dialect: net.sf.hibernate.dialect.SQLServerDialect
2004-5-5 11:49:33 net.sf.hibernate.cfg.SettingsFactory buildSettings
信息: Use outer join fetching: true
2004-5-5 11:49:33 net.sf.hibernate.connection.DriverManagerConnectionProvider configure
信息: Using Hibernate built-in connection pool (not for production use!);
2004-5-5 11:49:33 net.sf.hibernate.connection.DriverManagerConnectionProvider configure
信息: Hibernate connection pool size: 20
2004-5-5 11:49:34 net.sf.hibernate.connection.DriverManagerConnectionProvider configure
信息: using driver: com.microsoft.jdbc.sqlserver.SQLServerDriver at URL: jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=blueRain
2004-5-5 11:49:34 net.sf.hibernate.connection.DriverManagerConnectionProvider configure
信息: connection properties: {user=sa, password=}
2004-5-5 11:49:34 net.sf.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
信息: No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended);
2004-5-5 11:49:39 net.sf.hibernate.cfg.SettingsFactory buildSettings
信息: Use scrollable result sets: true
2004-5-5 11:49:39 net.sf.hibernate.cfg.SettingsFactory buildSettings
信息: Use JDBC3 getGeneratedKeys();: false
2004-5-5 11:49:39 net.sf.hibernate.cfg.SettingsFactory buildSettings
信息: Optimize cache for minimal puts: false
2004-5-5 11:49:39 net.sf.hibernate.cfg.SettingsFactory buildSettings
信息: Query language substitutions: {}
2004-5-5 11:49:39 net.sf.hibernate.cfg.SettingsFactory buildSettings
信息: cache provider: net.sf.ehcache.hibernate.Provider
2004-5-5 11:49:39 net.sf.hibernate.cfg.Configuration configureCaches
信息: instantiating and configuring caches
2004-5-5 11:49:40 net.sf.hibernate.impl.SessionFactoryImpl <init>
信息: building session factory
2004-5-5 11:49:42 net.sf.hibernate.impl.SessionFactoryObjectFactory addInstance
信息: no JNDI name configured
2004-5-5 11:49:43 net.sf.hibernate.util.JDBCExceptionReporter logWarnings
警告: SQL Warning: 0, SQLState: 
2004-5-5 11:49:43 net.sf.hibernate.util.JDBCExceptionReporter logWarnings
警告: [Microsoft][SQLServer 2000 Driver for JDBC]Database changed to blueRain
2004-5-5 11:49:43 net.sf.hibernate.util.JDBCExceptionReporter logWarnings
警告: SQL Warning: 0, SQLState: 
2004-5-5 11:49:43 net.sf.hibernate.util.JDBCExceptionReporter logWarnings
警告: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]已将数据库上下文改为 'blueRain'。
2004-5-5 11:49:43 net.sf.hibernate.util.JDBCExceptionReporter logWarnings
警告: SQL Warning: 0, SQLState: 
2004-5-5 11:49:43 net.sf.hibernate.util.JDBCExceptionReporter logWarnings
警告: [Microsoft][SQLServer 2000 Driver for JDBC]Language changed to 简体中文
2004-5-5 11:49:43 net.sf.hibernate.util.JDBCExceptionReporter logWarnings
警告: SQL Warning: 0, SQLState: 
2004-5-5 11:49:43 net.sf.hibernate.util.JDBCExceptionReporter logWarnings
警告: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]已将语言设置改为 简体中文。



到此请查看数据的person表,它已加入一条记录。

最后,看看提示信息可以理解更多hibernate运行的机制。让我们开始hibernate之旅吧
0 请登录后投票
   发表时间:2004-05-05  
要把lib中的jar文件加入classpath中,就不会出现这样那样的问题了!!!
0 请登录后投票
   发表时间:2004-05-05  
答应过smallduzi兄对他的例子Sqlserver版的细致说明
到现在才整理实是不好意思

写在前面的话:
回看出现过的错误,一般都是缺库文件所导致。附件图是我试出来的最少必需库清单。再不行可以把所有的库都加上。另msSQLJDBCDriver.jar是我把MS提供的三个JAR文件,重新打包成一个JAR。如果不用TangHan插件,可以就加入原来的三个JAR文件。

首先:代码当然是按原来的建立了。

  2 :
加入所需要的库文件,见下图:

 

所要加入的库文件没有显示出来啊,能不能告诉我所需的最小的库文件?我将所有的包都加入了,程序没有问题,不过我觉得这样不是太好(有些没必要的包也加入了);
0 请登录后投票
   发表时间:2004-05-06  
javalurker 写道

所要加入的库文件没有显示出来啊,能不能告诉我所需的最小的库文件?我将所有的包都加入了,程序没有问题,不过我觉得这样不是太好(有些没必要的包也加入了);


在最下面的图中就可以看到啊,原我想把图跟在第2点后面的,但不太会排版。
0 请登录后投票
   发表时间:2004-05-07  
liuyxit 写道

在最下面的图中就可以看到啊,原我想把图跟在第2点后面的,但不太会排版。



你能不能把它列出来啊?最下面的那张图我看不出用到了哪几个包
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics