当前位置: 首页 > 面试题库 >

未映射实体[从类型o中选择o]

乐正涵忍
2023-03-14
问题内容

我正在尝试使用hibernate创建一个小项目,但出现了错误“类型未映射[从类型o中选择o]”,我在hibernate.cfg.xml中添加了映射,但仍然出错。

Type.java:

package com.formation.gestionprojet.doa.entity;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;


@Entity
@Table(name="Type")
public class Type implements Serializable {
/**
 * 
 */
private static final long serialVersionUID = 1L;

@Id
private Long id;

private String name;

private String description;

private String active;




public Type() {
    super();
    // TODO Auto-generated constructor stub
}

public Long getId() {
    return id;
}

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

public String getName() {
    return name;
}

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

public String getDescription() {
    return description;
}

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

public String getActive() {
    return active;
}

public void setActive(String active) {
    this.active = active;
}

}

hibernate.org.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">

<hibernate-configuration>


<session-factory>

<!--  database connection setting -->

<property name ="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/gestion_projet?createDatabaseIfNotExist=true</property>
<property name="connection.username">root</property>
<property name= "connection.password">root</property>

<!-- Dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- Disable the second level cache -->

<property name="cache.provider_class" >org.hibernate.cache.NoCacheProvider</property>

<!-- echo all executed SQL to stdout -->

<property name="show_sql">true</property>

<!-- Drope and re-create the database -->
<property name="hbm2ddl.auto">update</property>

<!-- mapping -->


<mapping class= "com.formation.gestionprojet.doa.entity.Type"/>


</session-factory>

</hibernate-configuration>

hibernateUtil.java:

package com.formation.gestionprojet.utils;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

@SuppressWarnings("deprecation")
public class HibernateUtil
{
    private static SessionFactory sessionFactory;
    private static ServiceRegistry serviceRegistry;

    static
    {
        try
        {

            Configuration configuration = new Configuration();
            configuration.configure("config/hibernate.cfg.xml");

            serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        }
        catch (HibernateException ex)
        {
            System.err.println("Error creating Session: " + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory()
    {
        return sessionFactory;
    }



    public static Session openSession()
    {
        return sessionFactory.openSession();
    }


    public static Session getCurrentSession()
    {
        return sessionFactory.getCurrentSession();
    }


    public static void close(){
        if(sessionFactory!=null){
            sessionFactory.close();
        }

    }


}

测试Java

package com.formation.gestionprojet.utils;

import org.hibernate.Session;



public class Test {

    static Session session = HibernateUtil.openSession();

    public static void main(String[] args) {

        session.createQuery("select o from Type o").list();



    }

}

问题答案:

解决了 !问题是我使用的hibernate版本,所以我对其进行了更改,即HibernateUtil中的更改。



 类似资料:
  • 我在Kotlin-vertx项目中配置了Hibernate,我设法设置了所有内容,但当我运行HQL查询时,它会输出: 提前谢了。

  • 我正在spring应用程序中使用graphql java工具。我有这样一个实体: 在GraphQL模式中,我想要一个不同的类型: 当然,由于模式解析异常,启动此操作将不起作用。所以我认为在模式解析之间放置一些东西来实现从到的映射是可能的。 这有可能吗? 编辑 抱歉,它不会给出架构解析异常。它将简单地将字节放入字符串中。但是我不能用这个字符串来创建图像。 澄清 对不起,我想我的问题还不够清楚。我不需

  • 当我尝试通过枚举将源中的字符串映射到目标中的整数时。ModelMapper失败。 来源 目的地 字符串和整数之间的映射在枚举中定义

  • 我想映射类主题到主题表。 主题。JAVA 主题。哈佛商学院。xml 冬眠cfg。xml 我正在读取csv文件的内容,并希望使用以下代码将其插入数据库。 管理ata.java 我得到以下错误 线程“main”组织中出现异常。冬眠MappingException:未知实体:组织中的主题。冬眠impl。SessionFactoryImpl。getEntityPersister(SessionFactor

  • 是否有可能不在HiberNate中的实体层次结构中间为类创建表? 我想指出某些实体是某种类型的子集,以便返回所有这些实体的集合,但我不会在此中间类型中放置任何额外的属性。因此,保留带有id字段的额外表只是为了连接表,这听起来并不好。 更好的解决方案是实现一些公共接口< code>CommonInterface,但是这样我就失去了返回< code>List的可能性

  • 我目前正在尝试使用ModelMapper的映射方法执行实体到DTO的映射。在最深的属性映射上,映射结果为null,但它应该是一个对象。 以下是我的映射源实体(省略brevety的Loombok getter和setter): 以下是我的目标DTO(省略brevety的Loombok Getter和Setter): 在我的Controller类方法上,我这样做映射: My clientService