gpt4 book ai didi

java - Hibernate 映射错误 - 未知实体

转载 作者:行者123 更新时间:2023-12-01 16:40:21 27 4
gpt4 key购买 nike

我是一个 hibernate 新手,在尝试持久化对象时收到此错误 -

错误-->未知实体:org.apache.struts.register.model.Event

在我的hibernate.cfg.xml ,我将映射定义为,

<mapping class="org.apache.struts.register.model.Event"/>

我的事件类 -

package org.apache.struts.register.model;

import org.hibernate.annotations.Entity;


import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.Table;

@Entity
@Table (name="event")

public class Event {

@Id
@GeneratedValue ( strategy = GenerationType.AUTO)
private int eventId;
private String eventName;
private String eventDescription;

public String getEventName() {
return eventName;
}

public void setEventName(String eventName) {
this.eventName = eventName;
}

public String getEventDescription() {
return eventDescription;
}

public void setEventDescription(String eventDescription) {
this.eventDescription = eventDescription;
}

public String toString(){
return this.eventName + "," + this.eventDescription;
}

}

EventDAO -

package dao;


import org.apache.struts.register.model.Event;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;


public class EventDAO {

public static boolean registerEvent(Event u) {

SessionFactory sf = HibernateUtil.getSessionFactory();
Transaction t = null;
try {
Session s = sf.openSession();
t = s.beginTransaction(); // start a new transaction
s.persist(u);
t.commit(); // commit transaction
return true;
}
catch(Exception ex) {
System.err.println("Error -->" + ex.getMessage());
if ( t!=null) t.rollback(); // rollback transaction on exception
return false;
}
}
}

System.err.println("Error -->" + ex.getMessage()) ,是抛出异常的行。

谢谢。

最佳答案

import org.hibernate.annotations.Entity;

hibernate @Entity 不足以让您的实体被发现。请改用javax.persistence.Entity

See here :

@javax.persistence.Entity is still mandatory, @org.hibernate.annotations.Entity is not a replacement.

关于java - Hibernate 映射错误 - 未知实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4323066/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com