二月 14, 2017 3:43:38 下午 org.hibernate.annotations.common.Version <clinit> INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final} 二月 14, 2017 3:43:38 下午 org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {4.2.4.Final} 二月 14, 2017 3:43:38 下午 org.hibernate.cfg.Environment <clinit> INFO: HHH000206: hibernate.properties not found 二月 14, 2017 3:43:38 下午 org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021: Bytecode provider name : javassist 二月 14, 2017 3:43:38 下午 org.hibernate.cfg.Configuration configure INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml 二月 14, 2017 3:43:38 下午 org.hibernate.cfg.Configuration getConfigurationInputStream INFO: HHH000040: Configuration resource: /hibernate.cfg.xml 二月 14, 2017 3:43:39 下午 org.hibernate.cfg.Configuration addResource INFO: HHH000221: Reading mappings from resource: Students.hbm.xml 二月 14, 2017 3:43:39 下午 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide! 二月 14, 2017 3:43:39 下午 org.hibernate.cfg.Configuration doConfigure INFO: HHH000041: Configured SessionFactory: null 二月 14, 2017 3:43:39 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!) 二月 14, 2017 3:43:39 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000115: Hibernate connection pool size: 20 二月 14, 2017 3:43:39 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000006: Autocommit mode: false 二月 14, 2017 3:43:39 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost/hibernate] 二月 14, 2017 3:43:39 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000046: Connection properties: {user=root, password=****} 二月 14, 2017 3:43:39 下午 org.hibernate.dialect.Dialect <init> INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
首先Adress实体类 getter setter不全,会导致映射失败,另外报错光看这个有效信息太少,看出现红条的JUnit插件里面的报错会更准确目前看出来的就这些。
最后是Students.hb,.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"> <!-- Generated 2017-2-15 14:44:43 by Hibernate Tools 3.5.0.Final --> <hibernate-mapping package="default package"> <class name="Students" table="STUDENTS"> <id name="sid" type="int"> <column name="SID" /> <generator class="assigned" /> </id> <property name="sname" type="java.lang.String"> <column name="SNAME" /> </property> <property name="gender" type="java.lang.String"> <column name="GENDER" /> </property> <property name="birthday" type="java.util.Date"> <column name="BIRTHDAY" /> </property> <property name="picture" type="java.sql.Blob"> <column name="PICTURE" /> </property> <component name="address" class="Address"> <parent name="owner"></parent> <property name="postcode" ></property> <property name="phone"></property> <property name="address"></property> </component> </class> </hibernate-mapping>
然后是hibernate
<?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 name=""> <property name="connection.username">root</property> <property name="connection.password">tao</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost/hibernate</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 配置Hibernate数据库方言,Hibernate可针对特殊的数据库进行优化 --> <property name="show_sql">true</property> <!-- 是否把Hibernate运行时的SQL语句输出到控制台,编码阶段便于测试 --> <property name="format_sql">true</property> <!-- 输出到控制台的SQL语句是否进行排版,便于阅读。建议设置为TRUE --> <property name="hbm2ddl.auto">update</property> <!-- 可以帮助由Java代码生成数据库脚本,进而生成具体的表结构。create|update|create-drop|validate| --> <mapping resource="Students.hbm.xml"/> </session-factory> </hibernate-configuration>
下面是ADDRESS类
//地址类 public class Address { //指向该ADRESS属性的容器实体(也即Students对象) private Students owner; private String postcade;//邮编 private String phone;//电话 private String address;//地址 public Address(){} /* (非 Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { return "Address [postcade=" + postcade + ", phone=" + phone + ", address=" + address + "]"; } public Address(String postcade, String phone, String address) { //super(); this.postcade = postcade; this.phone = phone; this.address = address; } /** * @return owner */ public Students getOwner() { return owner; } /** * @param owner 要设置的 owner */ public void setOwner(Students owner) { this.owner = owner; } /** * @return postcade */ public String getPostcade() { return postcade; } /** * @param postcade 要设置的 postcade */ public void setPostcade(String postcade) { this.postcade = postcade; } /** * @return phone */ public String getPhone() { return phone; } /** * @param phone 要设置的 phone */ public void setPhone(String phone) { this.phone = phone; } /** * @return address */ public String getAddress() { return address; } /** * @param address 要设置的 address */ public void setAddress(String address) { this.address = address; } }
然后是Students
import java.sql.Blob; import java.util.Date; //持久化类,学生类 public class Students { /** * 编写持久化类(实体类),持久化类的设计原则要遵循javabean的设计原则,设计原则有四点: * 1. 这个类是一个公有类 * 2.提供公有的不带参数的默认的构造方法 * 3. 属性要私有化private * 4. 属性要用setter/getter封装 */ private int sid;// 学号 private String sname;// 姓名 private String gender;// 性别 private Date birthday;// 出生日期 //private String address;// 地址 private Blob picture;//照片 private Address address;//地址 /** * @return address */ public Address getAddress() { return address; } /** * @param address 要设置的 address */ public void setAddress(Address address) { this.address = address; } public Students(){} public Students(int sid, String sname, String gender, Date birthday, String address) { super(); this.sid = sid; this.sname = sname; this.gender = gender; this.birthday = birthday; //this.address = address; } public Students(int sid, String sname, String gender, Date birthday, String address, Blob picture) { super(); this.sid = sid; this.sname = sname; this.gender = gender; this.birthday = birthday; //this.address = address; this.picture = picture; } /* (非 Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { return "Students [sid=" + sid + ", sname=" + sname + ", gender=" + gender + ", birthday=" + birthday + ", address=" + address + ", picture=" + picture + "]"; } /** * @return sid */ public int getSid() { return sid; } /** * @param sid 要设置的 sid */ public void setSid(int sid) { this.sid = sid; } /** * @return sname */ public String getSname() { return sname; } /** * @param sname 要设置的 sname */ public void setSname(String sname) { this.sname = sname; } /** * @return gender */ public String getGender() { return gender; } /** * @param gender 要设置的 gender */ public void setGender(String gender) { this.gender = gender; } /** * @return birthday */ public Date getBirthday() { return birthday; } /** * @param birthday 要设置的 birthday */ public void setBirthday(Date birthday) { this.birthday = birthday; } /** * @return picture */ public Blob getPicture() { return picture; } /** * @param picture 要设置的 picture */ public void setPicture(Blob picture) { this.picture = picture; } }
首先是 StudentsTest import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FilterOutputStream; import java.io.IOException; import java.io.InputStream; import java.sql.Blob; import java.sql.SQLException; import java.util.Date; import org.hibernate.Hibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import org.junit.After; import org.junit.Before; import org.junit.Test; //测试类 public class StudentsTest { private SessionFactory sessionFactory; private Session session; private Transaction transaction; @Before public void init(){ //创建配置对象 Configuration config = new Configuration().configure(); //创建服务注册对象 ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); //创建会话工厂对象 sessionFactory = config.buildSessionFactory(serviceRegistry); //创建会话对象 session = sessionFactory.openSession(); //开启事物 transaction = session.beginTransaction(); } @Test public void testSaveStudents(){ //生成学生对象 //Students s = new Students(4,"张三","男",new Date(),"武当"); Students s = new Students(); s.setSid(111); s.setSname("张三丰"); s.setGender("男"); s.setBirthday(new Date()); // s.setAddress("武当山"); Address address = new Address("710064","02988567123","西安市"); s.setAddress(address); session.save(s);//保存对象进入数据库 } // @Test // public void testWriteBlob() throws IOException{ // //Students s = new Students(1,"张三丰","男",new Date(),"武当山"); // //先获得照片文件 // Students s = new Students(); // s.setSid(2); // s.setSname("啊哈哈"); // s.setGender("女"); // s.setBirthday(new Date()); // s.setAddress("武当山"); // File f= new File("D:"+File.separator+"下载"+File.separator+"一护.jpg"); // //获得文件的输入流 // FileInputStream fis =new FileInputStream(f); // Blob image =Hibernate.getLobCreator(session).createBlob(fis, fis.available()); // //设置照片属性 // s.setPicture(image); // //保存学生 // session.save(s); // // } //// @Test // public void testReadBlob() throws Exception{ // Students s = (Students) session.get(Students.class, 1); // //获得Blob对象 // Blob image = s.getPicture(); // //获得照片的输入流 // InputStream input = image.getBinaryStream(); // BufferedInputStream bis = new BufferedInputStream(input); // // //创建输出流 // File f = new File("D:"+File.separator+"下载"+File.separator+"DataBaseCopy.jpg"); // BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f)); // /** // * 方法一,一次性写入一个数组 // */ // byte [] buf = new byte[input.available()]; // bis.read(buf); // bos.write(buf); // /** // * 方法二,多次写入一个数组 // * 切记一定要刷新缓冲区,否则会出现字节数组丢失的问题 // */ // byte [] buf =new byte [1024]; // int i; // while((i=bis.read(buf))!=-1){ // bos.write(buf); // bos.flush(); // } // } @After public void destory(){ transaction.commit();//提交事务 session.close();//关闭会话 sessionFactory.close();//关闭会话工厂 } }
要不你就把包改成命名包把。。。还有就是哪个步骤错了?你的说啊