Hibernate + Spring exception: Unknown Entity
i am getting exception while the server starts. (Server is started using Intelij IDE). i have no idea how to fix it. i am new to hibernate and spring. thanks in advance.
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'authenticationMgr' defined in URL [jar:file:/C:/Program%20Files%20(x86)/Apache%20Software%20Foundation/Tomcat%207.0/webapps/ROOT/WEB-INF/lib/dbservice-1.0-SNAPSHOT.jar!/ApplicationContext-Service.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'authenticationDao' threw exception; nested exception is org.springframework.orm.hibernate3.HibernateSystemException: Unknown entity: com.jsi.core.dbservice.model.Authentication; nested exception is org.hibernate.MappingException: Unknown entity: com.jsi.core.dbservice.model.Authentication
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1361)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:871)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4681)
at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5184)
at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5179)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
at java.lang.Thread.run(Thread.java:619)
Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'authenticationDao' threw exception; nested exception is org.springframework.orm.hibernate3.HibernateSystemException: Unknown entity: com.jsi.core.dbservice.model.Authentication; nested exception is org.hibernate.MappingException: Unknown entity: com.jsi.core.dbservice.model.Authentication
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:102)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:58)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1358)
... 21 more
Authentication entity
/**
* Authentication Entity - Representation of the db table
*/
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
@Entity
@Table(name = "t_authentication")
public class Authentication extends LongBaseEntity implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "auth_id")
private Long mAuthId;
@Column(name = "authentication_id")
private Long mAuthenticationId;
@Column(name = "tcn")
private Long mTcn;
@Column(name = "audit_comment")
private String mAuditComment;
@Column(name = "last_timestamp")
private Date mLastTimeStamp;
@Column(name = "user_id")
private Long mUserId;
@Column(name = "authentication_date")
private Date mAuthenticationDate;
@Column(name = "hostname")
private String mHostname;
@Column(name = "ip_address")
private String mIpAddress;
@Column(name = "referrer_url")
private String mReferrerURL;
@Column(name = "session_id")
private String mSessionId;
public Long getAuthId() {
return mAuthId;
}
public void setAuthId(Long pAuthId) {
this.mAuthId = pAuthId;
mId = pAuthId;
}
public Long getAuthenticationId() {
return mAuthenticationId;
}
public void setAuthenticationId(Long pAuthenticationId) {
this.mAuthenticationId = pAuthenticationId;
}
public Long getTcn() {
return mTcn;
}
public void setTcn(Long pTcn) {
this.mTcn = pTcn;
}
public String getAuditComment() {
return mAuditComment;
}
public void setAuditComment(String pAuditComment) {
this.mAuditComment = pAuditComment;
}
public Date getLastTimeStamp() {
return mLastTimeStamp;
}
public void setLastTimeStamp(Date pLastTimeStamp) {
this.mLastTimeStamp = pLastTimeStamp;
}
public Long getUserId() {
return mUserId;
}
public void setUserId(Long pUserId) {
this.mUserId = pUserId;
}
public Date getAuthenticationDate() {
return mAuthenticationDate;
}
public void setAuthenticationDate(Date pAuthenticationDate) {
this.mAuthenticationDate = pAuthenticationDate;
}
public String getHostname() {
return mHostname;
}
public void setHostname(开发者_Python百科String pHostname) {
this.mHostname = pHostname;
}
public String getIpAddress() {
return mIpAddress;
}
public void setIpAddress(String pIpAddress) {
this.mIpAddress = pIpAddress;
}
public String getReferrerURL() {
return mReferrerURL;
}
public void setReferrerURL(String pReferrerURL) {
this.mReferrerURL = pReferrerURL;
}
public String getSessionId() {
return mSessionId;
}
public void setSessionId(String pSessionId) {
this.mSessionId = pSessionId;
}
public String toString() {
return "Payment{" +
"mId=" + getId() +
", mIpaddress=" + mIpAddress +
'}';
}
}
DAO class:
/**
* Implementation for AuthenticationMgr DAO layer.
*
*/
import com.jsi.core.dbservice.model.Authentication;
import com.jsi.core.dbservice.model.JSIException;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import java.sql.SQLException;
import java.util.List;
public class AuthenticationDao extends HibernateDaoSupport implements IAuthenticationDao {
@Override
public List<Authentication> list() {
final String query = "Select a from Authentication a order by a.id desc";
return (List<Authentication>) getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
return session.createQuery(query).list();
}
});
}
@Override
public void save(Authentication authentication) throws JSIException {
getHibernateTemplate().save(authentication);
}
@Override
public Authentication load(Long id) {
return getHibernateTemplate().load(Authentication.class, id);
}
@Override
public void update(Authentication authentication) throws JSIException {
getHibernateTemplate().update(authentication);
}
@Override
public void delete(Long id) {
getHibernateTemplate().delete(load(id));
}
}
If you are happened to use HibernateUtil to manipulate the data, you need to add annotated class to your configuration.
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new AnnotationConfiguration().addAnnotatedClass(Authentication.class)
.configure()
.buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
http://docs.jboss.org/hibernate/stable/annotations/reference/en/html/ch01.html
thanks everyone for your reply. i managed to solve it. it was my mistake. i forgot to add the mapping tag in xml.
<hibernate-configuration>
<session-factory>
<mapping class="com.model.Authentication"/> // i missed this line. after i added it. it worked fine.
</session-factory>
</hibernate-configuration>
thanks again.
精彩评论