Issue
I got this error when executing the main class. Am trying to insert new record at the table portfolio. As you see am using Hibernate ORM model to do that. Also it is good to say that hibernate doing the connection to the database without any problem !
Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.nortal.vspa.model.Portfolio
at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1129)
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1402)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:117)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:204)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:189)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:756)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:748)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:744)
at com.nortal.vspa.main.Main.main(Main.java:24)
Entity Class:
@Entity
@Table(name="portfolio")
public class Portfolio implements Serializable {
private static final long serialVersionUID = -1136208004146721604L;
@Id
@Column(name="symbol")
private String symbol;
public String getSymbol()
{
return symbol;
}
public void setSymbol(String symbol)
{
this.symbol = symbol;
}
}
Main Class:
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Portfolio portfolio = new Portfolio();
portfolio.setSymbol("MB");
session.save(portfolio);
session.getTransaction().commit();
}
}
Solution
The Hibernate configuration file must list the entity classes:
<mapping class="com.foo.bar.Portfolio"/>
Or you must explicitely add the class to the configuration using
configuration.addClass(com.foo.bar.Portfolio.class)
Answered By - JB Nizet