首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

hibernate新手有关问题哪个格小弟我解决下

2012-01-23 
hibernate新手问题哪个格我解决下啊Hibernate.cfg.xml?xml version1.0 encodingutf-8?!DOCTYPE h

hibernate新手问题哪个格我解决下啊
Hibernate.cfg.xml
 

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD3.0//EN" "hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
<property name="connection.url">jdbc:firebirdsql:[//host[:port]/]&lt;database&gt;</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<!-- 将Hibernate发送给数据库的sql显示出来 -->
<property name="show_sql">true</property>
<!-- Mapping files -->
<property name="myeclipse.connection.profile">SQL</property>
<mapping resource="model/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
----------------------------------------------------------------------
 HibernateSessionFactory.java


package util;
import model.*;
import org.hibernate.*;
import org.hibernate.cfg.*;

public class HibernateSessionFactory {
private static SessionFactory factory;
static 
{
Configuration cfg=new Configuration();
cfg.addClass(User.class);
factory=cfg.buildSessionFactory();
}
public static Session getSession()
{
return factory.openSession();
}

}
-------------------------------------------------

Dao.java


package util;
import model.*;
import org.hibernate.*;
import org.hibernate.cfg.*;



public class Dao {

public void insertUser(User user)throws Exception
{
Session session=null;
Transaction tran=null;
try
{
session=HibernateSessionFactory.getSession();
tran=session.beginTransaction();
session.save(user);
tran.commit();
}
catch(Exception e)
{
tran.rollback();
throw e;
}finally
{
if(session!=null)
{
session.close();
}
}


}
public void updateUser(User user)throws Exception
{
Session session=null;
Transaction tran=null;
try
{
session=HibernateSessionFactory.getSession();
tran=session.beginTransaction();
session.saveOrUpdate(user);
tran.commit();
}
catch(Exception e)
{
tran.rollback();
throw e;
}finally
{
if(session!=null)
{
session.close();
}
}
}
public User queryUserById(String userID)throws Exception
{
Session session=null;

try
{
session=HibernateSessionFactory.getSession();

User user=(User)session.load(User.class,userID);
return user;

}
finally
{
if(session!=null)
{
session.close();
}
}
}
public void deleteUser(String userID)throws Exception
{
Session session=null;
Transaction tran=null;
try
{
session=HibernateSessionFactory.getSession();
tran=session.beginTransaction();
session.delete(userID, User.class);
tran.commit();
}
catch(Exception e)
{
tran.rollback();
throw e;
}finally
{
if(session!=null)
{
session.close();
}
}

}

}
------------------------------------------------------



User.java


package model;

public class User {

private String userID;
private String username;
private String password;
private String per;

public void User(String userID,String username ,String password,String per)
{
this.userID=userID;
this.username=username;
this.password=password;
this.per=per;
}
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPer() {
return per;
}
public void setPer(String per) {
this.per = per;
}

}
----------------------------------------------------------
User.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
  Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
  <class name="User" table="db_user">
  <id name="userID" type="java.lang.String">
  <column name="userID" />
  <generator class="native" /><!-- 主键自动生成策略-->
  </id>
  <property name="username" type="java.lang.String">
  <column name="username" length="10" />
  </property>
  <property name="password" type="java.lang.String">
  <column name="password" length="20" />
  </property>
  <property name="per" type="java.lang.String">
  <column name="per" length="10" />
  </property>
   
  </class>
   
</hibernate-mapping>
-------------------------------------------------
Text.java



package main;
import model.*;
import util.*;

public class Test {

/**
* @param args
* @throws Exception 
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Dao userdao=new Dao();
User user=new User();
user.setUsername("小白");

user.setPassword("fsfsdfs");
user.setPer("a");

userdao.insertUser(user);
}

}

数据库名为db_YX 表名db_user 4个字段在User.hbm.xml里 数据库用户名sa 密码空;

哪位帮我解决下啊!!
在线等待中~!!!!!





[解决办法]
在创建 sessionfactory 是不是应该先调用个configure()方法?:
Configuration cfg=new Configuration();
factory=cfg.configure().buildSessionFactory();

热点排行