xuebing3249 2016-06-14 07:17 采纳率: 100%
浏览 1264

Hibernate 无法通过注解方式获取实体

org.hibernate.MappingException: Unknown entity: com.me.pojo.Cat
at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1096)
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1488)
at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:242)
at org.hibernate.event.internal.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:521)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:116)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:75)
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:811)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:784)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:789)

hibernate.cfg.xml



oracle.jdbc.driver.OracleDriver
tiger
jdbc:oracle:thin:@localhost:1521:ORCL
scott
org.hibernate.dialect.Oracle10gDialect
true

sessionfactory获取方式:
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry= new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);

import java.math.BigDecimal;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**

  • Cat generated by hbm2java
    */
    @Entity
    @Table(name = "CAT", schema = "SCOTT")
    public class Cat implements java.io.Serializable {

    private BigDecimal id;
    private String name;
    private String description;
    private Date createdate;
    private BigDecimal motherId;

    public Cat() {
    }

    public Cat(BigDecimal id) {
    this.id = id;
    }

    public Cat(BigDecimal id, String name, String description, Date createdate, BigDecimal motherId) {
    this.id = id;
    this.name = name;
    this.description = description;
    this.createdate = createdate;
    this.motherId = motherId;
    }

    @Id

    @Column(name = "ID", unique = true, nullable = false, precision = 22, scale = 0)
    public BigDecimal getId() {
    return this.id;
    }

    public void setId(BigDecimal id) {
    this.id = id;
    }

    @Column(name = "NAME", length = 32)
    public String getName() {
    return this.name;
    }

    public void setName(String name) {
    this.name = name;
    }

    @Column(name = "DESCRIPTION", length = 100)
    public String getDescription() {
    return this.description;
    }

    public void setDescription(String description) {
    this.description = description;
    }

    @Temporal(TemporalType.DATE)
    @Column(name = "CREATEDATE", length = 7)
    public Date getCreatedate() {
    return this.createdate;
    }

    public void setCreatedate(Date createdate) {
    this.createdate = createdate;
    }

    @Column(name = "MOTHER_ID", precision = 22, scale = 0)
    public BigDecimal getMotherId() {
    return this.motherId;
    }

    public void setMotherId(BigDecimal motherId) {
    this.motherId = motherId;
    }

}

我在网上查了很多都是说的注解包引用错了,但是我的注解包引用的都是javax.persistence.*;请各位大神指点

  • 写回答

3条回答

  • 乐古 2016-06-14 07:22
    关注

    1,检查你的映射文件的名字是否和你的pojo(*.java)的名字是否相同。
    2.映射文件的名字是*.hbm.xml而不是*.xml
    3.你是否加载了你的映射文件。
    加载的方法有两种
    (1)在你的Hibernate.cfg.xml配置文件中加载映射文件
    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">


    …………
    …………
    <!-- 用于记载映射文件Student.hbm.xml-->



    (2)在你的测试代码的加载映射文件
    Configuration cfg = new Configuration();
    cfg.configure();
    cfg.addClass(*.class); 这里的*.class是你的映射文件的名字*.hbm.class中的*。。。
    但是你要注意,用第二种方法加载的时候,你的*.hbm.class文件必须位于classpath下面。

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog