testWriteBlob空指針異常?
org.hibernate.boot.MappingNotFoundException: Mapping (RESOURCE) not found : Students.hbm.xml : origin(Students.hbm.xml)
at org.hibernate.boot.spi.XmlMappingBinderAccess.bind(XmlMappingBinderAccess.java:56)
at org.hibernate.boot.MetadataSources.addResource(MetadataSources.java:274)
at org.hibernate.boot.cfgxml.spi.MappingReference.apply(MappingReference.java:70)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:413)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
at org.hibernate.boot.MetadataSources.buildMetadata(MetadataSources.java:179)
at hibernate_demo01.StudentsTest.init(StudentsTest.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
java.lang.NullPointerException
at hibernate_demo01.StudentsTest.destroy(StudentsTest.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:37)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
實體類:
package hibernate_demo01;
import java.util.Date;
import java.sql.*;
//學生類
public class Students {
private int sid;//學號
private String sname;//姓名
private String gender;//性別
private Date birthday;//出生日期
private String address;//地址
private Blob picture;//圖片
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 int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Blob getPicture() {
return picture;
}
public void setPicture(Blob picture) {
this.picture = picture;
}
//為方便測試寫一個toString方法
@Override
public String toString() {
return "Students [sid=" + sid + ", sname=" + sname + ", gender=" + gender + ", birthday=" + birthday
+ ", address=" + address + "]";
}
}
hibernate配置:
<hibernate-configuration>
?<session-factory>
? <property name="hibernate.connection.username">root</property>
? <property name="hibernate.connection.password">qxy2972222</property>
? <property name="hibernate.connection.url">jdbc:mysql:///hibernate?useUnicode=true&amp;characterEncoding=UTF-8</property>
? <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
? <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
? <property name="hibernate.show_sql">true</property>
? <property name="hibernate.format_sql">true</property>
? <property name="hibernate.hbm2ddl.auto">create</property>
? <property name="hibernate.connection.autocommit">true</property>
? <property name="hibernate.default_schema">hibernate</property>
? <mapping resource="Students.hbm.xml"/>
?</session-factory>
</hibernate-configuration>
映射文件:
<hibernate-mapping>
? ? <class name="hibernate_demo01.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="address" type="java.lang.String">
? ? ? ? ? ? <column name="ADDRESS" />
? ? ? ? </property>
? ? ? ? <property name="picture" type="java.sql.Blob">
? ? ? ? ? ? <column name="PICTURE" />
? ? ? ? </property>
? ? </class>
</hibernate-mapping>
JUnit測試:
@Test
public void testWriteBlob() throws Exception{
Students s = new Students(1,"張三豐","男",new Date(),"武當山");
//先獲得照片文件,File.separator是文件分隔符\
File f = new File("g:"+File.separator+"TheMachineHeraldViktor.jpg");
//獲得照片文件的輸入流?,字節(jié)流用inputstream?
InputStream input = new FileInputStream(f);
//創(chuàng)建Blob對象,createBlob第一個參數表示從哪個輸入流讀取,第二個參數表示讀取的長度(輸入流可用字節(jié)數)
Blob image = Hibernate.getLobCreator(session).createBlob(input,input.available());
//設置照片屬性
s.setPicture(image);
//保存到學生
session.save(s);
}
2017-04-26
問題解決了,實體類添加picture屬性后,不知道為啥映射文件沒有放到src下。。。。。。
2017-04-25
我看了下mysql數據庫,確實沒有PICTURE這一列