hibernate+mysql问题

UserModel.java

package cn.javass.model;

public class UserModel {
 private Integer uuid;
 private String name;
 private Integer age;
 public Integer getUuid() {
  return uuid;
 }
 public void setUuid(Integer uuid) {
  this.uuid = uuid;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public Integer getAge() {
  return age;
 }
 public void setAge(Integer age) {
  this.age = age;
 }
 
}

 

UserModel.hbm.xml(我想配uuid自增)

<?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
 package="org.hibernate.auction">
 <class name="cn.javass.model.UserModel" table="tbl_user">
  <id name="uuid" unsaved-value="0">
   <generator class="increment"/>
  </id>
  <property name="name"/>
  <property name="age"/>
 </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.driver_class">org.gjt.mm.mysql.Driver</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/dhwebnms</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.connection.password">xxxxxx</property>
  
  <property name="show_sql">true</property>
  <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
  <mapping resource="cn/javass/model/UserModel.hbm.xml"/>  
 </session-factory>

</hibernate-configuration>

 

public class UserDao {
 public boolean  create(UserModel um,SessionFactory sf){
  Session se=null;
  Transaction tx=null;
  try{
   se=sf.openSession();
   tx=se.beginTransaction();
   se.save(um);
   tx.commit();
   se.close();
  }catch(Exception e){
   return false;
  }
  
  return true;
  
 }
 
 public List getAll(){
  return null;
 }
 
 public static void main(String[] args) {
  SessionFactory sessionFactory=null;
  try {
   sessionFactory=new Configuration().configure().buildSessionFactory();
   UserDao ud=new UserDao();
   UserModel um=new UserModel();
   um.setName("xyb");
   um.setAge(new Integer(10));
   ud.create(um, sessionFactory);
  } catch (HibernateException e) {
   e.printStackTrace();
  }
  
 }
}

 

 

运行后就报错,

net.sf.hibernate.MappingException: Error reading resource: cn/javass/model/UserModel.hbm.xml
 at net.sf.hibernate.cfg.Configuration.addResource(Configuration.java:336)
 at net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:990)
 at net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:946)
 at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:874)
 at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:860)
 at cn.javass.dao.UserDao.main(UserDao.java:38)
Caused by: net.sf.hibernate.MappingException: org.dom4j.DocumentException: Connection timed out: connect Nested exception: Connection timed out: connect
 at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:293)
 at net.sf.hibernate.cfg.Configuration.addResource(Configuration.java:333)
 ... 5 more
Caused by: org.dom4j.DocumentException: Connection timed out: connect Nested exception: Connection timed out: connect
 at org.dom4j.io.SAXReader.read(SAXReader.java:358)
 at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:283)
 ... 6 more

 

我不知道我的UserModel.hbm.xml的配制文件哪个写错了,请大家帮我看看.谢谢!

 


问题补充:
没有听明白
问题补充:
两位兄弟,我用的是hibernate2,pan_java回答的肯定不对,但是hibernate2怎么去配的就不知道了,请指教
问题补充:
兄弟们:
hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>  
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 2.0//EN"    
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">  
<hibernate-configuration>
    <session-factory>
        <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
        <property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>
        <property name="connection.username">root</property>
        <property name="connection.password">xybxyb</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/dhwebnms</property>

<property name="show_sql">true</property>
    </session-factory>

</hibernate-configuration>

就这样,然后我去SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
运行结果是;
net.sf.hibernate.HibernateException: problem parsing configuration/hibernate.cfg.xml
at net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:935)
at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:874)
at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:860)
at cn.javass.dao.UserDao.main(UserDao.java:41)
Caused by: net.sf.hibernate.MappingException: invalid configuration
at net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:931)
... 3 more
Caused by: org.xml.sax.SAXParseException: The content of element type "session-factory" is incomplete, it must match "(property*,mapping+,(class-cache|collection-cache|jcs-class-cache|jcs-collection-cache)*)".
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.handleEndElement(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.endElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanEndElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.dom4j.io.SAXReader.read(SAXReader.java:339)
at net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:930)
... 3 more
请指点


问题补充:
谢谢各位的帮忙,但是现在为什么插不进数据呀,运行不抛异常,数据库里面没有数据
<?xml version="1.0" encoding="utf-8"?>  
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 2.0//EN"    
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">  
<hibernate-configuration>
    <session-factory>
        <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
        <property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>
        <property name="connection.username">root</property>
        <property name="connection.password">xxxxxx</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/dhwebnms</property>
       
<property name="connection.pool.size">1</property>
<property name="statement_cache.size">25</property>
<property name="jdbc.fetch_size">50</property>
<property name="jdbc.batch_size">30</property>

<property name="show_sql">true</property>
<mapping resource="cn/javass/model/UserModel.hbm.xml" />

    </session-factory>

</hibernate-configuration>


UserModel.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
package="org.hibernate.auction">
<class name="cn.javass.model.UserModel" table="tbl_user">
  <id name="uuid" type="java.lang.Integer">
            <column name="uuid" />
            <generator class="native" />
        </id>
  <property name="name"/>
  <property name="age"/>
</class>
 
</hibernate-mapping>
运行的语句
SessionFactory sessionFactory=null;
try {

sessionFactory=new Configuration().configure().buildSessionFactory();
UserDao ud=new UserDao();
UserModel um=new UserModel();
um.setName("xyb");
um.setAge(new Integer(10));
ud.create(um, sessionFactory);
} catch (HibernateException e) {
e.printStackTrace();
}
数据库中没有值
问题补充:
不好意思,我说的不报错,其实错:错误为
se===>net.sf.hibernate.impl.SessionImpl@157aa53
net.sf.hibernate.JDBCException: Cannot open connection
at net.sf.hibernate.impl.BatcherImpl.openConnection(BatcherImpl.java:281)
at net.sf.hibernate.impl.SessionImpl.connect(SessionImpl.java:3302)
at net.sf.hibernate.impl.SessionImpl.connection(SessionImpl.java:3282)
at net.sf.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:40)
at net.sf.hibernate.transaction.JDBCTransactionFactory.beginTransaction(JDBCTransactionFactory.java:19)
at net.sf.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:2227)
at cn.javass.dao.UserDao.create(UserDao.java:22)
at cn.javass.dao.UserDao.main(UserDao.java:48)
Caused by: java.sql.SQLException: Communication failure during handshake. Is there a server running on localhost:3306?
at org.gjt.mm.mysql.MysqlIO.init(MysqlIO.java:327)
at org.gjt.mm.mysql.Connection.connectionInit(Connection.java:261)
at org.gjt.mm.mysql.jdbc2.Connection.connectionInit(Connection.java:89)
at org.gjt.mm.mysql.Driver.connect(Driver.java:167)
at java.sql.DriverManager.getConnection(DriverManager.java:525)
at java.sql.DriverManager.getConnection(DriverManager.java:140)
at net.sf.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:101)
at net.sf.hibernate.impl.BatcherImpl.openConnection(BatcherImpl.java:278)
... 7 more

问题补充:
我启动了mysql,也开启了事务
public class UserDao {
public boolean  create(UserModel um,SessionFactory sf){
Session se=null;
Transaction tx=null;
try{
se=sf.openSession();
System.out.println("se===>"+se);
tx=se.beginTransaction();
se.save(um);
tx.commit();
se.close();
}catch(Exception e){
e.printStackTrace();
return false;
}

return true;

}



public static void main(String[] args) {
SessionFactory sessionFactory=null;
try {

sessionFactory=new Configuration().configure().buildSessionFactory();
UserDao ud=new UserDao();
UserModel um=new UserModel();
um.setName("xyb");
um.setAge(new Integer(10));
ud.create(um, sessionFactory);
} catch (HibernateException e) {
e.printStackTrace();
}

}
}
问题补充:
mysql我重新启动了,没有问题,用的端口也3306,我安的mysql版本是mysql-5.1.34-win32.msi,我用mysql-gui-tools-5.0-r17-win32.msi我也不知道是不是mysql的版本原因
问题补充:
我觉得也是
我想问一下大家:我现在用的驱动是mm.mysql-2.0.4-bin.jar
我知道还有一个驱动是mysql-connector-java-3.0.16-ga-bin.jar这两者之间有什么区别,如果我用第二个,那么我的hibernate.cfg.xml真不好意思,麻烦大家这么长时间.
问题补充:
各们兄弟,问题找到了,从十点到现在一直在找这个问题,终于找到问题了,我换了一个驱动就行了,以前用mm.mysql-2.0.4-bin.jar ,现在改成了mysql-connector-java-3.0.16-ga-bin.j就能运行,在hibernate.cfg.xml里面改了一下就行了,特别感谢lovewhzlq兄弟.能留下你的qq吗,以后有什么问题请教你。但是为什么用mm.mysql-2.0.4-bin.jar 不行,我也不知道,如果谁知道了,请分享一下.
问题补充:
主要是项目中都用那个,我也不是领导,别人这样用,我只能这样用了

11个回答

你没有mysql 5.0对应的jdbc驱动吗?

去下一个嘛,这都是太旧的版本了

你用的是hibernate2.x版本,就应该用2.x版本的dtd

<?xml version="1.0"?>
[color=red]<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">[/color]
package="org.hibernate.auction">






[color=red]



[/color]
改成




uuid就会自增长了

数据库中 uuid 设置成 auto increment

数据库中 uuid 设置成 auto increment
uuid 设置成

不是让你把UserModel.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">
package="org.hibernate.auction">






The content of element type "session-factory" [color=red]is incomplete[/color],//session-factory元素配置的不完整, it must match "(property*,[color=red]mapping+,([/color]class-cache|collection-cache|jcs-class-cache|jcs-collection-cache)*)".

至少要有一个mapping元素

你都没启动事务

SessionFactory sessionFactory=null;
try {

sessionFactory=new Configuration().configure().buildSessionFactory();
[color=red]Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
UserDao ud=new UserDao();
UserModel um=new UserModel();
um.setName("xyb");
um.setAge(new Integer(10));
ud.create(um, sessionFactory);
tx.commit();
session.close();[/color]
} catch (HibernateException e) {
e.printStackTrace();
}

Caused by: java.sql.SQLException: Communication failure during handshake. Is there a server running on localhost:3306?

晕,你的mysql服务没启动??

事务是否提交

Caused by: java.sql.SQLException: Communication failure during handshake. Is there a server running on localhost:3306?

看这个错误,就是指你是否有一个mysql的服务运行在本机的3306端口上,

你看看mysql数据库的配置,端口正确不,

共11条数据 1 尾页
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐