7

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();

}

}
8
  • Have you listed that class in your hibernate configuration? Commented Dec 2, 2012 at 9:33
  • Is this the only entity in the application? Have you successfully persisted other entities? Commented Dec 2, 2012 at 9:33
  • @DavidWallace you mean to list them at persistence.xml ? Commented Dec 2, 2012 at 9:36
  • @kmb385 nope. there are another entities. I don't try them yet ! Commented Dec 2, 2012 at 9:37
  • 2
    So you expect me to cut and paste the solution from that page, because you don't have time to click a link? Commented Dec 2, 2012 at 9:55

2 Answers 2

29

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Does the order of the mapping class configurations matter? I have the mapping class listed in the configuration file, still the exception is coming.
0

This might be because of incorrect dialect name. In my case I have added server version number along with the MySQL dialect. ex. org.hibernate.dialect.MySQL8Dialect

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.