简体   繁体   English

如何从非主键表的休眠列表中选择数据,其中所有列的where子句数据都相同

[英]how to select data from hibernate list on non primary key table in which where clause data is same for all the column

I am new to hibernate framework,please provide your insight on this I want to populate list based on the where condition,but hibernate list() returns a redundant/duplicate data in the list instead of showing all the records based on the where clause in a non primary key table in which where clause data is same for all the column 我是hibernate框架的新手,请提供您对此的见解。我想根据where条件填充列表,但是hibernate list()返回列表中的冗余/重复数据,而不是根据中的where子句显示所有记录一个非主键表,其中所有列的where子句数据都相同

i don't have possibility to add primary key. 我没有添加主键的可能性。

Below is the code snippet 下面是代码片段

This is my hibernate.cfg.xml configuration file 这是我的hibernate.cfg.xml配置文件

<hibernate-configuration>
<session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/oracle</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>  
    <property name="show_sql">true</property>

    <mapping class="com.jspiders.app.dto.Office"/>
</session-factory>

This is my DTO class 这是我的DTO课程

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

@Entity
@Table(name="office")
public class Office {

    @Id
    @Column(name = "id")
    private int id;

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

    @Column(name = "column3")
    private String column3;

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getColumn3() {
        return column3;
    }

    public void setColumn3(String column3) {
        this.column3 = column3;
    }

    @Override
    public String toString() {
        return "Office [id=" + id + ", name=" + name + ", column3=" + column3 + "]";
}

This is my DAO class 这是我的DAO班

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;

import com.jspiders.app.dto.Office;
import com.jspiders.app.util.HibernateUtil;

public class OfficeDAO {
    SessionFactory factory = HibernateUtil.getUtility().getSessionFactory();

    public List<Office> retrieve() {
        Session s = factory.openSession();
        Query q = s.createQuery("select distinct o from Office o where id=1");
        List<Office> list = q.list();
        for (Office oo : list) {
            System.out.println(oo.toString());
        }
       return list;
    }

This is my singleton class 这是我的单身人士班

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

    private static final HibernateUtil util;
    private SessionFactory factory;

    static {
        util=new HibernateUtil();
    }

    private HibernateUtil()
    {
        Configuration cfg=new Configuration();
        cfg.configure();
        factory = cfg.buildSessionFactory();
    }


    public static HibernateUtil getUtility() {
        return util;
    }


    public  SessionFactory getSessionFactory() {
        return factory;
    }


    @Override
    protected void finalize() throws Throwable {

        if(factory!=null)
            factory.close();
    }


this snippet is from my main class
public static void main(String[] args) {

        OfficeDAO dao = new OfficeDAO();
        List<Office> list=dao.retrieve();

    }

this is my office table creation sql 这是我的办公室表创建sql

CREATE TABLE oracle.office
(
id INT NOT NULL,
NAME VARCHAR(30) NOT NULL,
column3 VARCHAR(30) NOT NULL
);

office table data 办公台数据

id name column3 1 a qwert 1 b abc id名称column3 1 a qwert 1 b abc

This is my Result 这是我的结果

Hibernate: select distinct office0_.id as id1_0_, office0_.column3 as column2_0_, office0_.name as name3_0_ from office office0_ where office0_.id=1 Office [id=1, name=abc, column3=qwert] Office [id=1, name=abc, column3=qwert] 休眠:从Office office0_中选择与众不同的office0_.id作为id1_0_,office0_.column3作为column2_0_,office0_.name作为name3_0_,其中office0_.id = 1 Office [id = 1,name = abc,column3 = qwert] Office [id = 1,名称= abc,第3列= qwert]

The problem is that in your entity you are specifying id as @Id . 问题是在您的实体中,您将id指定为@Id For that to work, it has to have an unique value for each row, which is not the case in your example data. 为此,每行必须具有唯一的值,示例数据中不是这种情况。 You could use a compound key for that. 您可以为此使用复合键。 Have something like: 有类似的东西:

public class OfficePK implements Serializable {
    private int id;
    private String name;

    @Column(name = "id")
    @Id
    public int getId() {
        return id;
    }

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

    @Column(name = "name")
    @Id
    public String getName() {
        return name;
    }

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

    //equals and hashcode
}

And the corresponding Office class would look like: 相应的Office类如下所示:

@Entity
@Table(name="office")
@IdClass(OfficePK.class)
public class Office {

    @Id
    @Column(name = "id")
    private int id;

    @Id
    @Column(name = "name")
    private String name;

    @Column(name = "column3")
    private String column3;

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getColumn3() {
        return column3;
    }

    public void setColumn3(String column3) {
        this.column3 = column3;
    }

    @Override
    public String toString() {
        return "Office [id=" + id + ", name=" + name + ", column3=" + column3 + "]";
}

You could of course also include column3 as part of the id. 您当然也可以将column3作为ID的一部分。

And btw. 顺便说一句。 you don't need to specify the column name if the name of the variable is the same as the column name. 如果变量名与列名相同,则无需指定列名。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从表中读取数据,其中使用休眠模式将主键合成 - Read data from table where primary key is composite using hibernate 如何使用从表中选择数据,进行计算,然后将新数据更新到另一个具有与第一个相同的主键的表中? - How to use select data from a table, make calculations and then updating new data into another table with same primary key as first? 带有外键的休眠表引用同一表的非主列不返回结果 - Hibernate table with foreign key referring to same table non-primary column returns no result 休眠-使用where子句选择-从域对象获取数据 - Hibernate — select with where clause — get data from domain object 如何 select 一个表中的所有数据,其中另一个表中的数据是另一个表中具有外键的东西 - How to select all the data from a table in which the data from another table is something having foreign key in another table 如何映射主键组件和外键具有相同列名的Hibernate实体? - How to map Hibernate entity where a primary key component and the foreign key have the same column name? 如何从具有多个(复合)主键的表中检索数据。 在休眠状态 - how to retrive data from the table that have multiple(composite) primary key. In Hibernate 通过在列表中应用where子句获取数据,该子句包含Hibernate中的专有数据类型元素 - Fetch data by applying where clause on list which contains premitive data type elements in Hibernate JPA 或 Hibernate 生成(非主键)列值,而不是从 1 开始 - JPA or Hibernate to generate a (non primary key) column value, not starting from 1 如何在执行 SELECT 之前为 WHERE 子句格式化列中的数据? - How to format data in column for WHERE clause just before executing SELECT?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM