java org.hibernate.MappingException:包含实体注释的用户对象上的未知实体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15528992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:57:09  来源:igfitidea点击:

org.hibernate.MappingException: Unknown entity on User object that contains Entity annotation

javaspringhibernatespring-mvc

提问by Jonathan

i am scratching my head wondering why this exception keeps popping up whenever i try to save a new user to my DB using spring and hibernate.

我正在挠头想知道为什么每当我尝试使用 spring 和休眠将新用户保存到我的数据库时,这个异常总是会弹出。

i have double checked and made sure @Entity annotation is present and even added @Table annoation to say which table this object belongs to and i am still recieving this error

我已经仔细检查并确保存在 @Entity 注释,甚至添加了 @Table 注释来说明此对象属于哪个表,但我仍然收到此错误

below is my user object:

下面是我的用户对象:

    @Entity
@Table(name = "USER")
public class User implements Serializable {

    /**
     * Serial version
     */
    private static final long serialVersionUID = 1L;

    @JsonElement(type = JsonElementType.JSON_KEY)
    public static final String KEY_BIO = "Bio";

    @JsonElement(type = JsonElementType.JSON_KEY)
    public static final String SURNAME = "Surname";

    @JsonElement(type = JsonElementType.JSON_KEY)
    public static final String KEY_FIRST_NAME = "FirstName";

    @JsonElement(type = JsonElementType.JSON_KEY)
    public static final String KEY_PASSWORD = "Password";

    @JsonElement(type = JsonElementType.JSON_KEY)
    public static final String KEY_EMAIL = "EmailAddress";

    @JsonElement(type = JsonElementType.JSON_KEY)
    public static final String KEY_USERNAME = "Username";

    @JsonElement(type = JsonElementType.JSON_KEY)
    public static final String KEY_USER_ID = "UserId";

    @JsonElement(type = JsonElementType.JSON_KEY)
    public static final int USERNAME_LENGTH = 5;

    @Id
    @Column(name = "USER_ID")
    @NotNull
    private String id;

    @Column(name = "USERNAME", nullable = false, unique = true)
    @NotNull
    private String username;

    @Column(name = "EMAIL_ADDRESS", nullable = false, unique = true)
    @NotNull
    private String emailAddress;

    @Column(name = "PASSWORD")
    @NotNull
    @Size(min = 5, max = 25)
    private String password;

    @Column(name = "FIRST_NAME")
    private String firstName;

    @Column(name = "SURNAME")
    private String surname;

    @Column(name = "COUNTRY")
    private String country;

    @Column(name = "BIO")
    private String bio;



    @Column(name = "SESSION_ID")
    private String sessionId;

    @Column(name = "LAST_REST_CALL_MADE")
    private Date lastRestCallMade;

    @Column(name = "STATUS")
    @NotNull
    private int status;

    @OneToOne
    @JoinColumn(name = "imageId")
    private ProfileImage profileImage;

    public User() {
        id = UUID.randomUUID().toString();
    }

    @JsonElement(type = JsonElementType.GETTER)
    public ProfileImage getProfileImage() {
        return profileImage;
    }

    @JsonElement(type = JsonElementType.SETTER)
    public void setProfileImage(ProfileImage profileImage) {
        this.profileImage = profileImage;
    }

    @JsonElement(type = JsonElementType.GETTER)
    public String getId() {
        return id;
    }

    @JsonElement(type = JsonElementType.SETTER)
    public void setId(String id) {
        this.id = id;
    }

    @JsonElement(type = JsonElementType.GETTER)
    public String getUsername() {
        return username;
    }

    @JsonElement(type = JsonElementType.SETTER)
    public void setUsername(String username) {
        this.username = username;
    }

    @JsonElement(type = JsonElementType.GETTER)
    public String getEmailAddress() {
        return emailAddress;
    }

    @JsonElement(type = JsonElementType.SETTER)
    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }

    @JsonElement(type = JsonElementType.GETTER)
    public String getPassword() {
        return password;
    }

    @JsonElement(type = JsonElementType.SETTER)
    public void setPassword(String password) {
        this.password = password;
    }

    @JsonElement(type = JsonElementType.GETTER)
    public String getFirstName() {
        return firstName;
    }

    @JsonElement(type = JsonElementType.SETTER)
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @JsonElement(type = JsonElementType.GETTER)
    public String getSurname() {
        return surname;
    }

    @JsonElement(type = JsonElementType.SETTER)
    public void setSurname(String surname) {
        this.surname = surname;
    }

    @JsonElement(type = JsonElementType.GETTER)
    public String getBio() {
        return bio;
    }

    @JsonElement(type = JsonElementType.SETTER)
    public void setBio(String bio) {
        this.bio = bio;
    }

    @JsonElement(type = JsonElementType.GETTER)
    public String getCountry() {
        return country;
    }

    @JsonElement(type = JsonElementType.SETTER)
    public void setCountry(String country) {
        this.country = country;
    }

    /**
     * @return the sessionId
     */
    public String getSessionId() {
        return sessionId;
    }

    /**
     * @param sessionId the sessionId to set
     */
    @JsonElement(type = JsonElementType.SETTER)
    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }

    /**
     * @return the lastRestCallMade
     */
    public Date getLastRestCallMade() {
        return lastRestCallMade;
    }

    /**
     * @param lastRestCallMade the lastRestCallMade to set
     */
    public void setLastRestCallMade(Date lastRestCallMade) {
        this.lastRestCallMade = lastRestCallMade;
    }

    /**
     * @return the status
     */
    public int getStatus() {
        return status;
    }

    /**
     * @param status the status to set
     */
    public void setStatus(int status) {
        this.status = status;
    }

}

here is what my userDao does

这是我的 userDao 所做的

@Override
    public void addNewUser(User user) throws InvalidDataException {
        // TODO: Add Transaction manager object here either using annototation
        // or creating it programatically

        if (mSessionFactory != null) {
            session = mSessionFactory.getCurrentSession();
            session.beginTransaction();
            if (user.getUsername() == null
                    || user.getUsername().length() < User.USERNAME_LENGTH) {
                throw new InvalidDataException(
                        InvalidDataException.ERROR_USERNAME_NULL);
            }

            session.save(user);

        }else{
            throw new RuntimeException("mSession is null");
        }
    }

pretty simple suff here.

这里很简单。

full stracktrace below:

完整的跟踪如下:

org.hibernate.MappingException: Unknown entity: com.jr.freedom.user.User
    at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1092)
    at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1399)
    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:753)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:745)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:741)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352)
    at $Proxy20.save(Unknown Source)
    at com.jr.freedom.dao.UserDao.addNewUser(UserDao.java:50)
    at com.jr.freedom.user.UserService.registerNewUser(UserService.java:47)
    at com.jr.freedom.controllers.UserController.createNewAccount(UserController.java:57)
    at com.test.jr.freedom.controllers.UserControllerTest.testCreateNewAccount(UserControllerTest.java:61)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access
<property name="annotatedClasses">
            <list>
                <value>com.jr.freedom.user.User</value>
                <value>com.jr.freedom.user.ProfileImage</value>
            </list>
        </property>
0(ParentRunner.java:50) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:222) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

it crashes when i invoke session.save(user);

当我调用 session.save(user) 时它崩溃了;

回答by Jonathan

Bingo. here is the issue that lies on my xml config file that configures the sessionFactory:

答对了。这是我配置 sessionFactory 的 xml 配置文件的问题:

<bean id="mySessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="configLocation" value="hibernate.cfg.xml" />
        <property name="annotatedClasses">
            <list>
                <value>com.jr.freedom.user.User</value>
                <value>com.jr.freedom.user.ProfileImage</value>
            </list>
        </property>

    </bean>

That was missing on this bean below:

下面这个 bean 上没有:

<mapping resource="com/swipex/backend/hibernate/CServiceCenters.hbm.xml" />

So i added that and it worked! question is, what if i have to maintain and use 100 of Entity's. it will be a chore creating a long list of annotatedClasses values inside this sessionFactory! Any idea how i can tell spring or hibernate to say "please scan through a selected package and find and use my Entity marked Objects".

所以我添加了它并且它起作用了!问题是,如果我必须维护和使用 100 个实体怎么办。在这个 sessionFactory 中创建一长串 annotatedClasses 值将是一件苦差事!知道我如何告诉 spring 或 hibernate 说“请扫描选定的包并找到并使用我的实体标记对象”。

回答by xyz

To answer question you posted in your answer.
You can use Entity Manager to autodetect hibernate entities.

回答您在回答中发布的问题。
您可以使用实体管理器自动检测休眠实体。

The class element specifies a fully qualified class name that you will map. By default all properly annotated classes and all hbm.xml files found inside the archive are added to the persistence unit configuration. You can add some external entity through the class element though

class 元素指定您将映射的完全限定类名。默认情况下,所有正确注释的类和存档中找到的所有 hbm.xml 文件都会添加到持久性单元配置中。您可以通过 class 元素添加一些外部实体

. http://docs.jboss.org/hibernate/stable/entitymanager/reference/en/html/configuration.html

. http://docs.jboss.org/hibernate/stable/entitymanager/reference/en/html/configuration.html

回答by Gimby

You can tell any JPA provider to scan a jar, but then you'd need to package all your entity classes in a separate jar. Any other solution is a provider specific hack. I did it by using google reflections to scan annotations and then use the EjbConfiguration class of Hibernate to register all entity annotated classes; but that is already deprecated in Hibernate 4 and is going to disappear in Hibernate 5.

您可以告诉任何 JPA 提供程序扫描 jar,但随后您需要将所有实体类打包在一个单独的 jar 中。任何其他解决方案都是特定于提供商的黑客。我是通过使用google反射扫描注解,然后使用Hibernate的EjbConfiguration类注册所有实体注解类来实现的;但这在 Hibernate 4 中已经被弃用,并将在 Hibernate 5 中消失。

回答by Siddharth

I forgot to add my hbm.xml to my cfg.xml file. As soon as I added it, this exception disappeared.

我忘了将我的 hbm.xml 添加到我的 cfg.xml 文件中。一旦我添加它,这个异常就消失了。

##代码##