跟着慕课网的hibernate初探之一对多映射,做到第一个实例就出错了下面是代码
hibernate.cfg.xml:
hibernate.cfg.xml:
<?xml version="1.0" encoding="utf-8"?> <!-- ~ Hibernate, Relational Persistence for Idiomatic Java ~ ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later. ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. --> <!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> <!-- SQL dialect --> <property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </property> <!-- Database connection settings --> <property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="hibernate.connection.url"> <![CDATA[ jdbc:mysql://localhost:3306/hibernatetest?useUnicode=true&characterEncoding=utf8 ]]> </property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">954552106</property> <property name="show_sql">true</property> <property name="dbm2ddl.auto">update</property> <mapping resource="com/ambition/entity/Grade.hbm.xml" /> <mapping resource="com/ambition/entity/Student.hbm.xml"/> </session-factory> </hibernate-configuration>
com.ambition.util.HibernateUtil.java:
package com.ambition.util; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static SessionFactory sessionFactory; private static Session session; static { //创建Configuration对象,读取hibernate.cfg.xml文件,完成初始化 Configuration configure = new Configuration().configure(); StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder() .applySettings(configure.getProperties()); StandardServiceRegistry ssr = ssrb.build(); sessionFactory = configure.buildSessionFactory(ssr); } public static SessionFactory getSessionFactory(){ return sessionFactory; } public static Session getSession(){ session = sessionFactory.openSession(); return session; } public static void closeSession(Session session){ if(session!=null){ session.close(); } } }
com.ambition.entity.Grade.java:
package com.ambition.entity; import java.io.Serializable; import java.util.HashSet; import java.util.Set; public class Grade implements Serializable { private int gid; private String gname; public Grade(String gname, String gdesc) { this.gname = gname; this.gdesc = gdesc; } public Grade() { } public int getGid() { return gid; } public void setGid(int gid) { this.gid = gid; } public String getGname() { return gname; } public void setGname(String gname) { this.gname = gname; } public String getGdesc() { return gdesc; } public void setGdesc(String gdesc) { this.gdesc = gdesc; } public Set<Student> getStudents() { return students; } public void setStudents(Set<Student> students) { this.students = students; } private String gdesc; private Set<Student> students = new HashSet<Student>(); }
com.ambition.entity.Student.java:
package com.ambition.entity; import java.io.Serializable; public class Student implements Serializable { private int sid; private String sname; private String sex; public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public Student(String sname, String sex) { super(); this.sname = sname; this.sex = sex; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
com.ambition.entity.Test.java:
package com.ambition.entity; import org.hibernate.Session; import org.hibernate.Transaction; import com.ambition.util.HibernateUtil; public class Test { public static void main(String[] args) { add(); } public static void add(){ Grade g = new Grade("java1班","java工程试验1班"); Student s1 = new Student("李明","男"); Student s2 = new Student("小红","女"); g.getStudents().add(s1); g.getStudents().add(s2); Session session = HibernateUtil.getSession(); Transaction t = session.beginTransaction(); session.save(g); session.save(s1); session.save(s2); t.commit(); HibernateUtil.closeSession(session); } }
com.ambition.entity.Grade.hbm.xml:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.ambition.entity.Grade" table="grade"> <id name="gid" column="gid" type="java.lang.Integer"> <generator class="increment"></generator> </id> <property name="gname" type="java.lang.String"> <column name="gname" length="20" not-null="true"></column> </property> <property name="gdesc"> <column name="gdesc"></column> </property> <!-- 配置单向的一对多关系 --> <set name="students" table="student"> <!-- 指定关联的外键 --> <key column="gid"></key> <one-to-many class="com.ambition.entity.Student"/> </set> </class> </hibernate-mapping>
com.ambition.entity.Student.hbm.xml:
<?xml version="1.0"?> <!-- ~ Hibernate, Relational Persistence for Idiomatic Java ~ ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later. ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. --> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.ambition.entity.Student" table="student"> <id name="sid" column="sid" type="java.lang.Integer"> <generator class="increment"></generator> </id> <property name="sname" type="java.lang.String"> <column name="sname" length="20" not-null="true"></column> </property> <property name="sex" type="java.lang.String"> <column name="sex"></column> </property> <!-- 配置多对一关联关系 --> <many-to-one name="grade" class="com.ambition.entity.Grade" column="gid" cascade="all"></many-to-one> </class> </hibernate-mapping>
错误如下:
一月 24, 2016 4:30:19 下午 org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {5.0.7.Final} 一月 24, 2016 4:30:19 下午 org.hibernate.cfg.Environment <clinit> INFO: HHH000206: hibernate.properties not found 一月 24, 2016 4:30:19 下午 org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021: Bytecode provider name : javassist 一月 24, 2016 4:30:19 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit> INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final} 一月 24, 2016 4:30:19 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!) 一月 24, 2016 4:30:19 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hibernatetest?useUnicode=true&characterEncoding=utf8] 一月 24, 2016 4:30:19 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001001: Connection properties: {user=root, password=****} 一月 24, 2016 4:30:19 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001003: Autocommit mode: false 一月 24, 2016 4:30:19 下午 org.hibernate.engine.jdbc.connections.internal.PooledConnections <init> INFO: HHH000115: Hibernate connection pool size: 20 (min=1) 一月 24, 2016 4:30:20 下午 org.hibernate.dialect.Dialect <init> INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect 一月 24, 2016 4:30:20 下午 org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl useContextualLobCreation INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4 Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.ambition.entity.Grade at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:781) at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1520) at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:100) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192) at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177) at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73) at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:679) at org.hibernate.internal.SessionImpl.save(SessionImpl.java:671) at org.hibernate.internal.SessionImpl.save(SessionImpl.java:666) at com.ambition.entity.Test.add(Test.java:23) at com.ambition.entity.Test.main(Test.java:12)
解决方案
40
本人跟你学的一个东西,找到解决方法了。本人也是hibernate5,StandardServiceRegistry ssr = ssrb.build();这句有问题改成StandardServiceRegistry ssr = ssrb..configure.build();不同版本存在细微差异,看一下说明文档。求互粉