2 u013260607 u013260607 于 2016.01.14 15:16 提问

初学Hibernate遇到问题,求解答~

运行一个hibernate例子,但是报错,小白不知道该怎么解决,麻烦大神帮看看。
我把代码贴出来~
(1).
package hibernate;
import org.hibernate.*;
import org.hibernate.cfg.*;
import org.hibernate.service.*;
import org.hibernate.boot.registry.*;
public class NewsManager
{
public static void main(String[] args)
throws Exception
{
Configuration conf = new Configuration()
.configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(conf.getProperties()).build();
SessionFactory sf = conf.buildSessionFactory(serviceRegistry);
Session sess = sf.openSession();
Transaction tx = sess.beginTransaction();
News n = new News();
n.setTitle("Java");
n.setContent("Java");
sess.save(n);
tx.commit();
sess.close();
sf.close();
}
}


(2).
package hibernate;
import javax.persistence.*;
@Entity
@Table(name="news_info")
public class News
{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
private String title;
private String content;
public void setId(Integer id)
{
this.id = id;
}
public Integer getId()
{
return this.id;
}
public void setTitle(String title)
{
this.title = title;
}
public String getTitle()
{
return this.title;
}
public void setContent(String content)
{
this.content = content;
}
public String getContent()
{
return this.content;
}
}


(3). 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">


org.gjt.mm.mysql.Driver
root
jdbc:mysql://localhost:3306/hibernate
root
org.hibernate.dialect.MySQL5InnoDBDialect



(标签贴不出来)


(4)错误信息
Exception in thread "main" org.hibernate.MappingException: Unknown entity: hibernate.News
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 hibernate.NewsManager.main(NewsManager.java:24)

1个回答

zy_281870667
zy_281870667   Ds   Rxr 2016.01.14 15:29

没看到你的扫描配置...

ramostear
ramostear 回复怒抄小纸条: 配置你的实体在什么地方,自动扫描的路径
接近 2 年之前 回复
zy_281870667
zy_281870667 回复怒抄小纸条: <!-- Hibernate核心对象 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <!--省略……--> <!--指定进行了Hibernate注解开发的包,方便框架进行扫描设置--> <property name="packagesToScan"> <list> <value>com/ljh/ppp/domain</value> </list> </property> </bean>
接近 2 年之前 回复
u013260607
u013260607 不不不不要走~~~~ 扫描神马配置? 00~
接近 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!