Exception in thread "main" org.hibernate.MappingException: Could not determine type for: String, for columns: [org.hibernate.mapping.Column(sname)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:266)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:253)
at org.hibernate.mapping.Property.isValid(Property.java:185)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:440)
at org.hibernate.mapping.RootClass.validate(RootClass.java:192)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1102)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1287)
at com.hibernate.Test.main(Test.java:13)
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.url">
jdbc:mysql://localhost:3306:hibernate01
</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.username">root</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.password"></property>
<mapping resource="com/hibernate/Student.hbm.xml" /></session-factory></hibernate-configuration>Student.hbm.xml:
<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" ><hibernate-mapping package="com.hibernate">
<class name="Student" table="student">
<id name="id" column="sid" type="int">
<generator class="increment"/>
</id>
<property name="username" column="sname" type="String"/>
<property name="age" column="sage" type="int"/>
</class>
</hibernate-mapping> 测试test:
package com.hibernate;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;public class Test {
public static void main(String[] args){
Configuration configuration=new Configuration();
configuration.configure("com/hibernate/hibernate.cfg.xml");//寻找配置文件
SessionFactory sessionFactory=configuration.buildSessionFactory();//产生session工厂
Session session=sessionFactory.openSession();//得到session对象
Transaction tx=session.beginTransaction();//产生事务,开启事务
Student zhangsan=new Student();
zhangsan.setUsername("zhangsan");
zhangsan.setAge(new Integer(12));
session.save(zhangsan);//存储命令
tx.commit();
session.close();
}
}Student类:
package com.hibernate;public class Student {
private String username;
private int age;
private int id;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}}
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:266)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:253)
at org.hibernate.mapping.Property.isValid(Property.java:185)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:440)
at org.hibernate.mapping.RootClass.validate(RootClass.java:192)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1102)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1287)
at com.hibernate.Test.main(Test.java:13)
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.url">
jdbc:mysql://localhost:3306:hibernate01
</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.username">root</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.password"></property>
<mapping resource="com/hibernate/Student.hbm.xml" /></session-factory></hibernate-configuration>Student.hbm.xml:
<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" ><hibernate-mapping package="com.hibernate">
<class name="Student" table="student">
<id name="id" column="sid" type="int">
<generator class="increment"/>
</id>
<property name="username" column="sname" type="String"/>
<property name="age" column="sage" type="int"/>
</class>
</hibernate-mapping> 测试test:
package com.hibernate;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;public class Test {
public static void main(String[] args){
Configuration configuration=new Configuration();
configuration.configure("com/hibernate/hibernate.cfg.xml");//寻找配置文件
SessionFactory sessionFactory=configuration.buildSessionFactory();//产生session工厂
Session session=sessionFactory.openSession();//得到session对象
Transaction tx=session.beginTransaction();//产生事务,开启事务
Student zhangsan=new Student();
zhangsan.setUsername("zhangsan");
zhangsan.setAge(new Integer(12));
session.save(zhangsan);//存储命令
tx.commit();
session.close();
}
}Student类:
package com.hibernate;public class Student {
private String username;
private int age;
private int id;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}}
映射的是数据库吗?数据库没这个类型有varchar吧。