[英]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.