开发者社区> 问答> 正文

想看hibernate官网上注解的配置demo,怎么找啊?报错

想看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)



我想问是配置不对吗?



展开
收起
爱吃鱼的程序员 2020-06-09 10:49:51 465 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB
    <mappingresource="Student"/>

    这里是不是应该写成class属性

    2020-06-09 10:50:07
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Java Spring Boot开发实战系列课程【第15讲】:Spring Boot 2.0 API与Spring REST Docs实战 立即下载
Java Spring Boot开发实战系列课程【第7讲】:Spring Boot 2.0安全机制与MVC身份验证实战(Java面试题) 立即下载
Java Spring Boot开发实战系列课程【第6讲】:Spring Boot 2.0实战MyBatis与优化(Java面试题) 立即下载