我做的第一个hibernate映射,出错了,大家帮我看下,有详细的说明
我的pojo:
package com.ren.po;
public class Cat {
private String id;
private String name;
public Cat() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
String strCat = new StringBuffer()
.append(this.getId()).append(", ")
.append(this.getName()).append(", ")
.toString();
return strCat;
}
}
---------------------------------------------------------------------------------
==Cat.hbm.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.ren.po.Cat"
table="cat"
dynamic-update="false"
>
<id
name="id"
column="cat_id"
type="string"
unsaved-value="any"
>
<generator class="assigned"/>
</id>
<property
name="name"
type="string"
update="true"
insert="true"
column="cat_name"
/>
</class>
</hibernate-mapping>
--------------------------------------------------------------------
===hibernate.cfg.xml==
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">
jdbc:mysql://localhost/test
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.username">
root
</property>
<property name="hibernate.connection.password">
2155596
</property>
<property name="dialect">
net.sf.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.show_sql">
True
</property>
<mapping resource="Cat.hbm.xml" />
</session-factory>
</hibernate-configuration>
---------------------------------------------------------
==Test.java==
package com.ren.po;
import java.sql.*;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
import java.io.*;
public class Test
{
public Test()
{
try
{
File file=new File("E:/javawork/com/ren/po/hibernate.cfg.xml");
Cat cat = new Cat();
cat.setId("003");
cat.setName("renkunpong");
SessionFactory sessionFactory;
Session session;
Configuration cfg=new Configuration();
sessionFactory=cfg.configure(file).buildSessionFactory();
session=sessionFactory.openSession();
session.save(cat);
session.close();
}
catch(Exception ex){ex.printStackTrace();}
}
public static void main(String[] a)
{
new Test();
}
}
-----------------------------------
后台信息
2004-12-1 3:24:50 net.sf.hibernate.cfg.Environment <clinit>
信息: Hibernate 2.1.2
2004-12-1 3:24:50 net.sf.hibernate.cfg.Environment <clinit>
信息: hibernate.properties not found
2004-12-1 3:24:50 net.sf.hibernate.cfg.Environment <clinit>
信息: using CGLIB reflection optimizer
2004-12-1 3:24:50 net.sf.hibernate.cfg.Configuration configure
信息: configuring from file: hibernate.cfg.xml
2004-12-1 3:24:50 net.sf.hibernate.cfg.Configuration addResource
信息: Mapping resource: Cat.hbm.xml
2004-12-1 3:24:50 net.sf.hibernate.cfg.Binder bindRootClass
信息: Mapping class: com.ren.po.Cat -> cat
2004-12-1 3:24:50 net.sf.hibernate.cfg.Configuration doConfigure
信息: Configured SessionFactory: null
2004-12-1 3:24:50 net.sf.hibernate.cfg.Configuration secondPassCompile
信息: processing one-to-many association mappings
2004-12-1 3:24:50 net.sf.hibernate.cfg.Configuration secondPassCompile
信息: processing one-to-one association property references
2004-12-1 3:24:50 net.sf.hibernate.cfg.Configuration secondPassCompile
信息: processing foreign key constraints
2004-12-1 3:24:50 net.sf.hibernate.dialect.Dialect <init>
信息: Using dialect: net.sf.hibernate.dialect.MySQLDialect
2004-12-1 3:24:50 net.sf.hibernate.cfg.SettingsFactory buildSettings
信息: Use outer join fetching: true
2004-12-1 3:24:50 net.sf.hibernate.connection.DriverManagerConnectionProvider co
nfigure
信息: Using Hibernate built-in connection pool (not for production use!)
2004-12-1 3:24:50 net.sf.hibernate.connection.DriverManagerConnectionProvider co
nfigure
信息: Hibernate connection pool size: 20
2004-12-1 3:24:50 net.sf.hibernate.connection.DriverManagerConnectionProvider co
nfigure
信息: using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost/test
2004-12-1 3:24:50 net.sf.hibernate.connection.DriverManagerConnectionProvider co
nfigure
信息: connection properties: {user=root, password=2155596}
2004-12-1 3:24:50 net.sf.hibernate.transaction.TransactionManagerLookupFactory g
etTransactionManagerLookup
信息: No TransactionManagerLookup configured (in JTA environment, use of process
level read-write cache is not recommended)
2004-12-1 3:24:50 net.sf.hibernate.cfg.SettingsFactory buildSettings
信息: Use scrollable result sets: true
2004-12-1 3:24:50 net.sf.hibernate.cfg.SettingsFactory buildSettings
信息: Use JDBC3 getGeneratedKeys(): true
2004-12-1 3:24:50 net.sf.hibernate.cfg.SettingsFactory buildSettings
信息: Optimize cache for minimal puts: false
2004-12-1 3:24:50 net.sf.hibernate.cfg.SettingsFactory buildSettings
信息: echoing all SQL to stdout
2004-12-1 3:24:50 net.sf.hibernate.cfg.SettingsFactory buildSettings
信息: Query language substitutions: {}
2004-12-1 3:24:50 net.sf.hibernate.cfg.SettingsFactory buildSettings
信息: cache provider: net.sf.ehcache.hibernate.Provider
2004-12-1 3:24:50 net.sf.hibernate.cfg.Configuration configureCaches
信息: instantiating and configuring caches
2004-12-1 3:24:50 net.sf.hibernate.impl.SessionFactoryImpl <init>
信息: building session factory
2004-12-1 3:24:51 net.sf.hibernate.impl.SessionFactoryObjectFactory addInstance
信息: no JNDI name configured
Press any key to continue...
--------------------------------------------------------------------------------
我用jCreator做的,Java.class,Cat.class,Cat.hbm.xml.hibernate.cfg.xml都放在一个包里面(com.ren.po),在jCreator里面把驱动程序的jar包,还有一些hibernate的jar包都引入了,运行的时候就出现上面的提示信息,可是在mysql数据库里面没有插入数据,大家帮我仔细看下,我做这个做了好几天了,可硬是做不出来
问题点数:100、回复次数:7Top
1 楼SBNOone(1)回复于 2004-12-01 03:34:38 得分 0
相应的数据库也建好了,就是不能成功,大家帮帮我啊555555555555Top
2 楼woodcord(我心飞翔)回复于 2004-12-01 07:35:35 得分 10
帮楼主顶一下Top
3 楼AndrewCSDN(无尽的永恒)回复于 2004-12-01 07:44:56 得分 0
要么用 session.flush(), 要么用Transaction,(最好用Trasactio) 这样才能保存。(注意 commit() 方法)
session=sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.save(cat);
tx.commit();
session.close();
sessionFactory.close();
Top
4 楼AndrewCSDN(无尽的永恒)回复于 2004-12-01 07:53:13 得分 80
为了方便楼主Hibernate的学习,顺便提一下。一般 hiberate.cfg.xml 是放在你的源文件最外层的
文件夹中的。 比如你的包 com/ren/po, 假设这个包是在 src/文件夹中,你的hibernate.cfg.xml 也应该放在 src/下, 相应的 hiberante.cfg.xml 要做改动
<mapping resource="com/ren/po/Cat.hbm.xml"/>
最后在 你的Test 文件中写
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final Configuration cfg = new Configuration();
------
//然后再用这个文件,这样就不用指定文件在哪个盘了
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
--------
最后,楼主要看一下相关的ThreadLocal Design Patten,记住要 close.sessionFactory()
Hibernate是非常好的一个技术,但是也是很难透彻理解的,一旦学好,收益很大。Top
5 楼fmzbj(mz)回复于 2004-12-01 09:05:16 得分 0
你去下一个在Eclipse下用的Hibernate的插件,一句代码都不用你写,何必这么痛苦呢!Top
6 楼tim90(Piece of my wish)回复于 2004-12-01 09:32:11 得分 10
Java.class,Cat.class,放一个包
Cat.hbm.xml.
hibernate.cfg.xml放类目录最外层Top
7 楼SBNOone(1)回复于 2004-12-01 14:27:27 得分 0
谢谢了,终于弄出来了Top




