gpt4 book ai didi

java - 运行 hibernate 5.0.1 程序时出现未知实体异常

转载 作者:行者123 更新时间:2023-12-01 10:19:47 25 4
gpt4 key购买 nike

在 Hibernate 5.0.1 上运行 Hibernate 程序时出现以下错误:

Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.abhinav.hibernate.UserDetails
at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:776)
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1542)
at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:225)
at org.hibernate.event.internal.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:499)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:99)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58)
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:778)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:751)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:756)
at com.abhinav.hibernate.main.HibernateTest.main(HibernateTest.java:26)

下面是配置文件:

<session-factory>

<!-- Database connection settings -->
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/hibernatedb</property>
<property name="connection.username">postgres</property>
<property name="connection.password">password</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>

<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>


<mapping resource="com.abhinav.hibernate.UserDetails"/>

</session-factory>

主类:

package com.abhinav.hibernate.main;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

import com.abhinav.hibernate.UserDetails;

public class HibernateTest {

public static void main(String[] args) {
// TODO Auto-generated method stub
UserDetails userDetails = new UserDetails();

userDetails.setUserId(1);
userDetails.setUserName("Abhinav");

Configuration configuration = new Configuration().configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
SessionFactory sessionFactory = configuration.buildSessionFactory(builder.build());
Session session = sessionFactory.openSession();
session.getTransaction();
System.out.println("Session: "+session);

// session.save(userDetails);
session.persist(userDetails);

System.out.println("Session after save: "+session+" User details: "+userDetails.getUserId()+" "+userDetails.getUserName());
session.getTransaction().commit();
}

实体类: UserDetails,其中包含字段 userIduserName

最佳答案

有两个问题

您需要使用

<mapping class="com.abhinav.hibernate.UserDetails"/>

如果您使用注释进行映射(resource 用于 xml 映射)。

您不能使用 Hibernate 4 方式为 Hibernate 5 构建 session 工厂。正确的方式:

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

详情请参阅

Hibernate 5 :- org.hibernate.MappingException: Unknown entity

关于java - 运行 hibernate 5.0.1 程序时出现未知实体异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35675456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com