Caused by: org.hibernate.MappingException: Could not determine type for: String,
无意中遇到这个异常:
解决办法: 在mapping时候,把类型写完整s
<property name="pname" column="pname" type="String"></property>
需要改成:
<property name="pname" column="pname" type="java.lang.String"></property>
开始Hibernate之旅:
1: 导入hibernate需要用到的包
2: 编写hibernate配置文件:hibernate.cfg.xml(位于src目录下s)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="connection.url"> jdbc:mysql://localhost:3306/mytest </property> <property name="connection.username">root</property> <property name="connection.password">123456</property> <!-- JDBC connection pool (use the built-in) --> <!--<property name="connection.pool_size">1</property>--> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache --> <property name="cache.provider_class"> org.hibernate.cache.NoCacheProvider </property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property> <mapping resource="bean/Persion.hbm.xml"/> </session-factory> </hibernate-configuration>
3.写实体类:实力类必须提供无参构造方法以及set get方法s
package bean; import java.util.Date; public class Persion { private int id; private String pname; private int age; private String email; private String telphone; private String birthday; public Persion() { } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getTelphone() { return telphone; } public void setTelphone(String telphone) { this.telphone = telphone; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } }
4.写映射文件:命名为Persion.hbm.xml(实体名.hbm.xmls)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class table="persion" name="bean.Persion"> <id column="id" name="id"> <generator class="native"></generator> </id> <property name="pname" column="pname" type="java.lang.String"></property> <property name="age" column="age" type="int"></property> <property name="email" column="email" type="java.lang.String"></property> <property name="telphone" column="telphone" type="java.lang.String"></property> <property name="birthday" column="birthday" type="String"></property> </class> </hibernate-mapping>
5.编写Hibernate工具类
package util; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { try { // Create the SessionFactory from hibernate.cfg.xml Configuration config = new Configuration().configure(); sessionFactory = config.buildSessionFactory(); return sessionFactory; } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.out.println("sessionFactory: "+sessionFactory); System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } }
6:编写测试类
package test; import org.hibernate.Session; import util.HibernateUtil; import bean.Persion; public class Test { public static void main(String[] args) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); Persion persion = new Persion(); persion.setPname("搁浅"); persion.setAge(23); persion.setBirthday("1989-05-28"); persion.setEmail("xiaowei123ymhr@126.com"); persion.setTelphone("******"); s session.save(persion); session.getTransaction().commit(); } }