HIbernate给出了未知的实体异常

时间:2016-07-01 02:39:49

标签: java eclipse hibernate

这是我的模型类(AppInfo,Userinfo)

@Entity
@Table(appliesTo="appinfo")
public class AppInfo  {

    @Id
    @Column(name="appid")
    private int appId;

    @Column(name="appname")
    private String appName;

    @Column(name="roleid")
    private int roleId;

    @OneToMany(fetch=FetchType.LAZY,targetEntity=UserInfo.class,cascade=CascadeType.ALL)
    @JoinColumn(name="Appid",referencedColumnName="appid")
    private Set<UserInfo> userInfo;

        // setters & getters
}

@Entity
@Table(appliesTo = "userinfo")
public class UserInfo {

    @Id
    @Column(name = "userid")
    private int userId;

    @Column(name = "username")
    private String userName;

            @Column(name = "hoid")
            private String hoId;
//setters & getters
    }

我的hbm配置文件

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/taskdb</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <mapping class="com.myproject.hibernate.model.UserInfo.java"></mapping>
        <mapping class="com.myproject.hibernate.model.AppInfo.java"></mapping>
    </session-factory>

我的实用程序类

   public class UtilityAnnotation {


                private static final SessionFactory sessionFactory = buildSessionFactory();

                @SuppressWarning

s("deprecation")
            private static SessionFactory buildSessionFactory() {
                try {
                    // Create the SessionFactory from hibernate.cfg.xml
                    return new AnnotationConfiguration().configure().buildSessionFactory();
                } catch (Throwable ex) {
                    // Make sure you log the exception, as it might be swallowed
                    System.err.println("Initial SessionFactory creation failed." + ex);
                    throw new ExceptionInInitializerError(ex);
                }
            }

            public static SessionFactory getSessionFactory() {
                return sessionFactory;
            }

            public static void shutdown() {
                // Close caches and connection pools
                getSessionFactory().close();
            }

    }

最后是主课

public class OneToMany {

    public static void main(String[] args) {
        Session session = UtilityAnnotation.getSessionFactory().openSession();
        session.beginTransaction();
        UserInfo info = new UserInfo();
        AppInfo ap = new AppInfo();

        ap.setAppId(1);
        ap.setAppName("Check");
        ap.setRoleId(1);

        info.setUserName("Grey");
        info.setUserId(4);
        info.setHoneyId("4");

        Set<UserInfo> mys = new HashSet<UserInfo>();
        mys.add(info);

        ap.setUserInfo(mys);

        session.save(ap);
        session.getTransaction().commit();
        session.close();
    }
}

当我运行主类Iget跟随错误

Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.myproject.hibernate.model.AppInfo
    at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:693)
    at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1485)
    at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:120)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
    at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
    at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
    at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:713)
    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:701)
    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:697)
    at com.myproject.hibernate.practice.OneToMany.main(OneToMany.java:32)

我搜索了解决方案,并且在许多人已经告诉要更改配置文件映射,其中类路径应该是正确的但我已经给出了正确的类路径,所以我在这里犯了什么错误。

我还有一个单独的主类,它将信息添加到userinfo中,并且运行正常。

3 个答案:

答案 0 :(得分:2)

如果要使用注释添加实体,则需要在配置对象中添加带注释的实体

new AnnotationConfiguration().configure().addAnnotatedClass(AppInfo.class).addAnnotatedClass(UserInfo.class).buildSessionFactory();
如果您使用的是xml配置,

还会从映射类xml配置中删除 .java

答案 1 :(得分:1)

替换

    <mapping class="com.myproject.hibernate.model.UserInfo.java"></mapping>
    <mapping class="com.myproject.hibernate.model.AppInfo.java"></mapping>

    <mapping class="com.myproject.hibernate.model.UserInfo"/>
    <mapping class="com.myproject.hibernate.model.AppInfo"/>

答案 2 :(得分:1)

使用Java来包含实体而不是xml,对于其他开发人员来说,它更易于维护和读取。

当前异常是由于映射类xml配置中的.java

如果要使用注释添加实体,请更喜欢在配置对象中添加带注释的实体

    new AnnotationConfiguration().configure().addAnnotatedClass(AppInfo.class).addAnnotatedClass(UserInfo.class).buildSessionFactory();