关于hibernate的使用,在看视频的时候,咯啊是是用User建的一个实体类,用来映射生成表的。可是,运行时老是报User关键字错误,这里申明一下,我用的是sql server2008的数据库。最后才发现原来User是我的数据库的关键字,事先自己无法理解数据库的为什么和User有关系,其实当我打开sql的权限的时候发现里面有一个用户的名字里包含了User,这就是包User错误的关键。
现在说说我与到的一些错误:
1.在读取hibernate.cfg.xml文件的时候,需要Configuration cfg = new Configuration().configure();也就是后面需要加上configure(),不然hibernate默认读取的是hibernate.properties文件。
2.hibernate.cfg.xml文件的放置的位置必须在src的目录之下,这个我们可以通过ctrl+s在点击configure()查看到默认的路径(事先要将hibernate的zip文件add进去)。
3.网上说hibernate对数据库进行了封装,在映射表时只能在控制台打出你的sql语句,其实在sql的数据库中是可以看到的。
4.hibernate的机制只能帮助我们生产表、所以当我们配置好hibernate.cfg.xml文件,就要根据我们的数据库的名字去建立我们的数据库,hibernate不会帮助我们生成数据库的。
5.配置hibernate.cfg.xml,我们会根据模板,我建议里面的sql的driver和url最好不要用模板的,最好用以前自己的jdbc的用过的
6.在建实体类的时候,我们会生成get和set的方法,注意要继承object类的构造方法。
我的一些文件的源代码:
hibernate.cfg.xml 》》》<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;DataBaseName=123</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<mapping resource="com/xx/www/Tea.hbm.xml"/>
</session-factory>
</hibernate-configuration>
ExportDb.java 》》》
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;DataBaseName=123</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<mapping resource="com/xx/www/Tea.hbm.xml"/>
</session-factory>
</hibernate-configuration>
package com.xx.www;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class ExportDb {
public static void main(String[] args) {
// TODO Auto-generated method stub
Configuration cfg = new Configuration().configure();
SchemaExport sc = new SchemaExport(cfg);
sc.create(true, true);
}
}
实体类Tea.java 》》》
package com.xx.www;
import java.util.Date;
public class Tea {
private String id;
private String name;
private Date createTime;
public Tea() {
super();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}
Tea.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="com.xx.www.Tea">
<id name="id" type="string">
<generator class="uuid"/>
</id>
<property name="name" type="string"/>
<property name="createTime" type="date"/>
</class>
</hibernate-mapping>
插入的类Client.java 》》》
package com.xx.www;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Client {
public static void main(String[] args) {
Configuration cfg = new Configuration().configure();
SessionFactory factory = cfg.buildSessionFactory();
Session session = null;
try {
session = factory.openSession();
session.beginTransaction();
Tea t = new Tea();
t.setCreateTime(new Date());
t.setName("张三");
session.save(t);
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally{
if(session != null){
if(session.isOpen()){
session.close();
}
}
}
}
}
package com.xx.www;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class ExportDb {
public static void main(String[] args) {
// TODO Auto-generated method stub
Configuration cfg = new Configuration().configure();
SchemaExport sc = new SchemaExport(cfg);
sc.create(true, true);
}
}
实体类Tea.java 》》》
package com.xx.www;
import java.util.Date;
public class Tea {
private String id;
private String name;
private Date createTime;
public Tea() {
super();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}
Tea.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="com.xx.www.Tea">
<id name="id" type="string">
<generator class="uuid"/>
</id>
<property name="name" type="string"/>
<property name="createTime" type="date"/>
</class>
</hibernate-mapping>
插入的类Client.java 》》》
package com.xx.www; import java.util.Date; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class Client { public static void main(String[] args) { Configuration cfg = new Configuration().configure(); SessionFactory factory = cfg.buildSessionFactory(); Session session = null; try { session = factory.openSession(); session.beginTransaction(); Tea t = new Tea(); t.setCreateTime(new Date()); t.setName("张三"); session.save(t); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally{ if(session != null){ if(session.isOpen()){ session.close(); } } } } }