4

执行主类时出现此错误。我试图在表投资组合中插入新记录。如您所见,我正在使用 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();

}

}
4

2 回答 2

25

Hibernate 配置文件必须列出实体类:

<mapping class="com.foo.bar.Portfolio"/>

或者您必须使用明确地将类添加到配置中

configuration.addClass(com.foo.bar.Portfolio.class)
于 2012-12-02T09:46:49.410 回答
0

这可能是因为方言名称不正确。就我而言,我添加了服务器版本号以及 MySQL 方言。前任。org.hibernate.dialect.MySQL8Dialect

于 2021-01-16T06:27:01.367 回答