Java代码:
package hello;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Message {
private Long id;
private String text;
private Message nextMessage;
private Message() {}
public Message(String text) {
this.text = text;
}
public Long getId() {
return id;
}
private void setId(Long id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Message getNextMessage() {
return nextMessage;
}
public void setNextMessage(Message nextMessage) {
this.nextMessage = nextMessage;
}
public static void main(String[] args){
Configuration conf = new Configuration();
conf.configure("hibernate.cfg.xml");
System.out.println("Conf Loaded Successfully");
Session session = conf.buildSessionFactory().openSession();
Transaction tx = session.beginTransaction();
Message message = new Message("Hello World");
session.save(message);
tx.commit();
session.close();
System.out.println("Written to DB successfully");
}
}
使用Hibernate 4.0.1自动生成Hibernate配置和XML文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Dec 5, 2015 9:20:05 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="hello.Message" table="MESSAGE">
<id name="id" type="java.lang.Long">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="text" type="java.lang.String">
<column name="TEXT" />
</property>
<one-to-one name="nextMessage" class="hello.Message"></one-to-one>
</class>
</hibernate-mapping>
Hibernate 4.0.1是我的eclipse插件。但我在xmlns中看到了3个。这是问题吗?
项目结构
hibernateFirst
src/main/java
hello
Message
src/main/resources
hello
Message.hbm.xml
hibernate.cfg.xml
错误:
Exception in thread "main" org.hibernate.MappingException: Unknown entity: hello.Message
at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:776)
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 hello.Message.main(Message.java:49)
请帮我解决。
我刚试过在映射中更改hello / Message。但它不起作用。
答案 0 :(得分:0)
xml配置中声明的类名只是&#34;消息&#34;,不需要&#34;你好&#34;
有关简单示例,请参阅http://www.tutorialspoint.com/hibernate/hibernate_examples.htm
在一对一的映射中也是如此。