执行主类时出现此错误。我试图在表投资组合中插入新记录。如您所见,我正在使用 Hibernate ORM 模型来做到这一点。还可以说休眠完成与数据库的连接没有任何问题!
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
@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;
}
}
主类:
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();
}
}