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 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制