想看hibernate官网上注解的配置demo,怎么找啊,下载了hibernate-release-5.2.2.Final.zip,解压后再project下的etc里也没有找到,在哪里找呀,或者直接告我配置是不是错了
这是实体类
import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator; /** * Student entity. @author MyEclipse Persistence Tools */ @Entity @Table(name = "student", catalog = "test") public class Student implements java.io.Serializable { // Fields private String tbId; private String tbName; private Integer tbAge; // Constructors /** default constructor */ public Student() { } /** minimal constructor */ public Student(String tbId) { this.tbId = tbId; } /** full constructor */ public Student(String tbId, String tbName, Integer tbAge) { this.tbId = tbId; this.tbName = tbName; this.tbAge = tbAge; } // Property accessors @GenericGenerator(name = "generator", strategy = "increment") @Id @GeneratedValue(generator = "generator") @Column(name = "tb_id", unique = true, nullable = false, length = 22) public String getTbId() { return this.tbId; } public void setTbId(String tbId) { this.tbId = tbId; } @Column(name = "tb_name", length = 12) public String getTbName() { return this.tbName; } public void setTbName(String tbName) { this.tbName = tbName; } @Column(name = "tb_age") public Integer getTbAge() { return this.tbAge; } public void setTbAge(Integer tbAge) { this.tbAge = tbAge; } }
下面是配置文件
<!-- ~ 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 name="foo"> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="myeclipse.connection.profile">Driver</property> <property name="show_sql">true</property> <!-- 在默认包下,所以直接写了类名,是可以点进去的 --> <mapping resource="Student"/> </session-factory> </hibernate-configuration>
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; public class Dao { public static void main(String[] args) { Configuration cfg = new Configuration().configure(); StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(cfg.getProperties()).build(); SessionFactory sessionFactory = cfg.buildSessionFactory(serviceRegistry); Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Student student = new Student("1", "小王", 21); session.save(student); transaction.commit(); session.close(); sessionFactory.close(); } }
九月 05, 2016 10:52:35 上午 org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {5.2.2.Final} 九月 05, 2016 10:52:35 上午 org.hibernate.cfg.Environment <clinit> INFO: HHH000206: hibernate.properties not found 九月 05, 2016 10:52:35 上午 org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021: Bytecode provider name : javassist 九月 05, 2016 10:52:35 上午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit> INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final} 九月 05, 2016 10:52:35 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!) 九月 05, 2016 10:52:35 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/test] 九月 05, 2016 10:52:35 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001001: Connection properties: {user=root, password=****} 九月 05, 2016 10:52:35 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001003: Autocommit mode: false 九月 05, 2016 10:52:35 上午 org.hibernate.engine.jdbc.connections.internal.PooledConnections <init> INFO: HHH000115: Hibernate connection pool size: 20 (min=1) 九月 05, 2016 10:52:35 上午 org.hibernate.dialect.Dialect <init> INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect Exception in thread "main" org.hibernate.MappingException: Unknown entity: Student at org.hibernate.metamodel.internal.MetamodelImpl.entityPersister(MetamodelImpl.java:620) at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1604) at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:104) 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:673) at org.hibernate.internal.SessionImpl.save(SessionImpl.java:665) at org.hibernate.internal.SessionImpl.save(SessionImpl.java:660) at Dao.main(Dao.java:17)
<mappingresource="Student"/>
这里是不是应该写成class属性
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。