简单的hibernate,高手指点.. 我是新手

zhouzongxi 2008-05-27 11:54:26

package hibernate.ch01;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.*;
//测试类
public class HibernateTest {

/**
* @param args
*/
public static void main(String[] args) {

SessionFactory sessions=new Configuration().configure().buildSessionFactory();
Session session=sessions.openSession();
Transaction tx=null;
try{
tx=session.beginTransaction();
UserInfo u=new UserInfo();
u.setUserName("zzx");
u.setPassword("123");
System.out.println("开始插入数据");
session.save(u);
UserInfo u1=(UserInfo)session.load(UserInfo.class, new Integer(1));
System.out.println("用户为:" +u1.getUserName());
tx.commit();
tx=null;
System.out.println("结束");
}
catch(Exception ex)
{
ex.printStackTrace();
if(tx!=null)
tx.rollback();
}
finally
{
session.close();
}
}
}

//================javabean========
package hibernate.ch01;

public class UserInfo {

private Integer id;
private String userName;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}

}
//=======UserInfo.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="hibernate.ch01.UserInfo" table="login">
<id name="id" type="integer">
<column name="id"/>
<generator class="identity"/>
</id>
<property name="userName" type="string">
<column name="name" length="100"/>
</property>
<property name="password" type="string">
<column name="password" length="100"/>
</property>
</class>
</hibernate-mapping>



//===hibernate.cfg.xml


<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

<session-factory>
<property name="connection.username">root</property>
<property name="connection.url">jdbc:mysql://localhost/hibernate</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.password">Zhouzongxi</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>

</session-factory>

</hibernate-configuration>

Myelicpse开发的驱动导入了,下面的是运行的结果

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
开始插入数据
org.hibernate.MappingException: Unknown entity: hibernate.ch01.UserInfo
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:569)
at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1086)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:83)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:481)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:476)
at hibernate.ch01.HibernateTest.main(HibernateTest.java:24)


//哪位能耐心的给我讲下么..自学的.



...全文
83 点赞 收藏 14
写回复
14 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
whetu 2008-05-28
这个错误提示,org.hibernate.MappingException: Unknown entity: hibernate.ch01.UserInfo
说明你的hibernate.cfg.xml 有问题,虽然你有映射文件,但是你的UserInfo并没有被映射。
在你的hibernate.cfg.xml中增加一个配置。如下:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

<session-factory>
<property name="connection.username">root </property>
<property name="connection.url">jdbc:mysql://localhost/hibernate </property>
<property name="dialect">org.hibernate.dialect.MySQLDialect </property>
<property name="connection.password">Zhouzongxi </property>
<property name="connection.driver_class">com.mysql.jdbc.Driver </property>
<mapping resource="hibernate/ch01/UserInfo.hbm.xml"/>
</session-factory>

</hibernate-configuration>
这样你的映射文件在hibernate启动时,才被加载。
试一下。
回复
zhouzongxi 2008-05-28
小熊猫。我按你的试了 也没用。
狒狒 我还不会Stping的 呵呵 刚学的 谢谢你们两位
回复
sea_way 2008-05-28
建议你用spring的HibernateTemplate来管理session
获取session
getHibernateTemplate().getSessionFactory().getCurrentSession()
这样用完不必关系session
回复
Shine_Panda 2008-05-28
session.save(u);
//加一句试下看
session.flush();

UserInfo u1=(UserInfo)session.load(UserInfo.class, new Integer(1));
回复
zhouzongxi 2008-05-28
谢谢你啦 搞定了,给你加分!!
我把那个UserInfo.hbm.xml改了下位置 提示其他的错误信息 ..后面解决了
回复
whetu 2008-05-28
没有出现“开始插入数据”是因为没有找到hbm.xml文件,
导致执行到SessionFactory sessions=new Configuration().configure().buildSessionFactory()时,出错,
只要找到hbm.xml,正确生成session,“开始插入数据”就出来了
回复
whetu 2008-05-28
还是目录不对,按你在10楼所说,你的主程序和hbm.xml在同一个目录,试一下这样
<mapping resource="UserInfo.hbm.xml"/>
回复
zhouzongxi 2008-05-28
sunchang_hong :你说的那个异常解决了..谢谢!


whetu:

路径也没错啊
Exception in thread "main" org.hibernate.MappingException: Resource: hibernate/ch01/UserInfo.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:447)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1263)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1235)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1217)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1184)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1112)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1098)
at hibernate.ch01.HibernateTest.main(HibernateTest.java:15)

相比之下 我第1个还打印出来了“开始插入数据”的字样,这个没有也...
回复
zhouzongxi 2008-05-28
hibernate
|
--src
|
--hibernate.ch01
¦
---hibernateTest.java
---UserInfo.java
---UserInfo.hbm.xml

回复
zhouzongxi 2008-05-28
hibernate
|
--hibernate.ch01
|
回复
suncheng_hong 2008-05-28
org.hibernate.MappingException: Unknown entity: hibernate.ch01.UserInfo
没有找到UserInfo实体,看看hbm文件指定目录下有没有这个类.
回复
whetu 2008-05-28
确保你的UserInfo.hbm.xml是在hibernate/ch01/目录下,也就是和UserInfo.java在一个目录下。
回复
zidasine 2008-05-28
Unknown entity
ls 正确

回复
zhouzongxi 2008-05-28
whetu:
按你说的是对的 ,现在又出现一个异常,说文件没找到..

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.MappingException: Resource: hibernate/ch01/UserInfo.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:447)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1263)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1235)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1217)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1184)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1112)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1098)
at hibernate.ch01.HibernateTest.main(HibernateTest.java:15)
请问下 这个应该怎么改?是不是我的目录弄错了?指点 谢谢
回复
相关推荐
发帖
Java EE
创建于2007-09-28

6.6w+

社区成员

J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
申请成为版主
帖子事件
创建了帖子
2008-05-27 11:54
社区公告
暂无公告