Hibernate老是mapping错误找不到实体类,用的是sql2008!大神快来帮帮我。

时间:2021-10-29 14:11:11
hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>

<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="connection.password">123</property>
<property name="connection.username">sa</property>
<property name="connection.url">
jdbc:sqlserver://localhost:1433;DatabaseName=ceshi
</property>
<property name="connection.driver_class">
com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.show_sql">true</property>

<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.min_size">1</property>
<property name="hibernate.c3p0.timeout">5000</property>
<property name="hibernate.c3p0.max_statements">100</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<property name="hibernate.c3p0.acquire_increment">2</property>
<property name="hibernate.c3p0.validate">true</property>
<property name="javax.persistence.validation.mode">none</property>


<mapping resource="com/chpt10/model/News.hbm.xml" />
</session-factory>

</hibernate-configuration>



package com.chpt10.model;

public class News {
private int id;
private String title;
private String content;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

}


News.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hiberante-mapping>
<class name="com.chpt10.model.News" table="News">
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="title" type="string"/>
<property name="content"/>
</class>
</hiberante-mapping>

执行程序

package com.chpt10.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.chpt10.model.News;

public class NewsOperator {

public static void main(String[] args) {
// 采用默认的hibernate.cfg.xml来启动一个Configuration的实例
Configuration cfg = new Configuration().configure();
// 以Configuration实例来创建SessionFactory实例
SessionFactory sFactory = cfg.buildSessionFactory();
Session session = sFactory.openSession();
Transaction transaction = session.beginTransaction();
News news = new News();
news.setTitle("标题1!!");
news.setContent("第一条内容!!!!!");
session.save(news);
transaction.commit();
session.close();
}

}


错误!

Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.chpt10.model.News
at org.hibernate.metamodel.internal.MetamodelImpl.entityPersister(MetamodelImpl.java:620)
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1627)
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:682)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:674)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:669)
at com.chpt10.test.NewsOperator.main(NewsOperator.java:22)

5 个解决方案

#1


<class name="com.chpt10.model.News" table="News">改一下试试<class package="com.chpt10.model" name="News">

#2


<class package="com.chpt10.model" name="News">

#3


改改试试,我发了几次怎么看不到内容

#4


看配置文件没什么问题,你用的是hibernate什么版本的?

#5


该回复于2017-04-28 10:19:23被管理员删除

#1


<class name="com.chpt10.model.News" table="News">改一下试试<class package="com.chpt10.model" name="News">

#2


<class package="com.chpt10.model" name="News">

#3


改改试试,我发了几次怎么看不到内容

#4


看配置文件没什么问题,你用的是hibernate什么版本的?

#5


该回复于2017-04-28 10:19:23被管理员删除