I have a ArrayList which of type empDetail (a POJO class).
List<EmpDetail> empDetailList = new ArrayList<EmpDetail>();
This list represents a table in the database.
I need values for a dropdown list and so I did
<s:select list="empDetailList" listKey="country" listValue="country" name="country"></s:select>
By this I get all rows of country column from database and its good. But I need unique country in this dropdown list.
I know I can write a SQL query to get distinct country, but how to do it in this kind of scenario.
Update 1:
Do any one of you have Hibernate solution for this?
Update 2:
My POJO class as follows...
package bean;
import java.io.Serializable;
import java.sql.Date;
public class EmpDetail implements Serializable{
private static final long serialVersionUID = 1L;
private Long id;
private String name;
private int age;
private String address;
private String city;
private String section;
private String country;
private String classStudying;
private String fatherName;
private String motherNmae;
private Date DOJ;
private String certificates;
private Date CompletedDate;
private String crossCheckedBy;
private Date crossCheckedDate;
private String comments;
//and its getters and setters...
}
You need to group by country objects. Simple HQL query
from EmpDetail where id in (select max(id) from EmpDetail group by country)
If you want to display distinct select items means
EmpDetail empDetail = new EmpDetail();
Map<Object, Object> empDetailMap = new LinkedHashMap<Object, Object>();
if (empDetailList.size() > 0) {
for (Iterator<EmpDetail> iter = industryDetail.iterator(); iter.hasNext();) {
empDetail = (EmpDetail) iter.next();
empDetailMap.put(empDetail.getId(),empDetail.getName());
}
}
Related
Hi is there a way where we can do this via HQL??
where it returns me list of DTO instead of list of Object Class.
My sql query is
select * from readings join
(select max(signal) as signal,sender as sr ,receiver as rc from readings group by readings.sender,readings.receiver)a
on
a.signal=readings.signal and
a.sr=readings.sender and
a.rc=readings.receiver
Here is my DTO/Bean/Pojo class
public class Readings implements java.io.Serializable {
private Integer id;
private String sender;
private String major;
private String minor;
private int signal;
private BigDecimal power;
private BigDecimal temperature;
private String battery;
private String receiver;
private Date createdDatetime;..
and getters and setters ...
Which in HQL is
Criteria cr = session.createCriteria(com.XYZ..Readings.class)
.setProjection(Projections.projectionList()
.add(Projections.max("signal"))
.add(Projections.groupProperty("sender"))
.add(Projections.groupProperty("receiver")));
List<Readings> br=(List<Readings>)cr.list();
This fails at the last line, trying to cast Object to Readings class.
I want to save the _id in Review class as an ObjectId but when I query it I want it to be displayed as a String and not the {"timestamp", "machineIdentifier", etc.}
Here are the classes I made:
#Document
public class Catalog {
....
private List<Review> reviews;
....
}
public class Review {
#Id
private ObjectId _id = new ObjectId();
private String userid;
private String username;
private String reviewstring;
}
The class Review in the mongodb is an embedded document.
When I query for a certain review with the _id as a String, it returns null. How can I display the ObjectId as a String during queries but let it remain as an ObjectId?
#JsonSerialize(using = ToStringSerializer.class)
Description:
I have two tables, Shop_Employee and Shop_Employee_Type. I wanna display typeName directly when show the employee detail information, but I don't want to config the relationship(OneToMany or ManyToOne) between this two entity. Because this will load all Shop_Employee_Type column's value, but these values are useless for me, I just need typeName of Shop_Employee_Type.
Below is my code, but it doesn't work.
ShopEmployeeType:
#Entity
#Data
//#DynamicUpdate
public class ShopEmployeeType {
#Id
private String typeId;
private String shopId;
private String typeName;
private Integer typeStatus;
private String typeDescription;
}
Shop_Employee:
#Entity
#Data
public class ShopEmployee {
#Id
private String employeeId;
private String shopId;
private String typeId;
private String name;
private String code;
private String phone;
private Integer status;
private String idcardNumber;
private String image;
//#Transient
private String typeName;
public ShopEmployee() {
}
}
Repository:
#Query(value = "select u.*,t.type_name from shop_employee u inner join shop_employee_type t on u.type_id=t.type_id", nativeQuery = true)
List<ShopEmployee> findAllData();
This could show typeName as I wished, but there is an error appears when I save a new entity Shop_Employee; If I add a #Transient for 'typeName', It could save successfully, but the value of 'typeName' is null when I query entity Shop_Employee.
Your query should return two Objects Shop_Employee and a String, so the return results should not be List<ShopEmployee> it should be :
#Query(value = "select u.*, t.type_name from shop_employee ...", nativeQuery = true)
List<Object[]> findAllData();
Then you can get the ShopEmployee using :
List<Object[]> list = findAllData();
for(Object[] obj : list){
ShopEmployee shopEmployee = (ShopEmployee) obj[0];
String type_name = (String) obj[1];
}
So, in ShopEmployee entity you don't need to use :
//#Transient
//private String typeName;
The following query throws the exception:
Query query = session.createQuery("from Associate as a order by a.username asc");
associates = query.list();
org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [ca.mypkg.model.Associate#0]
If I create an entry in the database with id of 0 it works just fine. I don't really get it because I'm just trying to load all the entries in the db not just a specific one.
Similar questions I've found have been concerned with trying to load an object with a given ID I'm doing no such thing.
Associate class:
#Table(name = "user")
#XmlRootElement(name = "associate")
public class Associate implements Serializable {
private String username;
private String password;
private String firstName;
private String lastName;
private String userType;
private int id;
private String email;
private String isActive;
private Department dept;
private String lastUpdated;
private String associate_type;
// ...
#Id
#GeneratedValue
public int getId() {
return id;
}
#OneToOne
#JoinColumn(name = "dept")
public Department getDept() {
return dept;
}
From my experience this type of error message usually means it does not find joined entity by mentioned id, and not the entity requested in the query (Associate, in your case).
My guess is that Associate class contains a join entity which has primitive type primary key.
I'm using DBUtils in my simple project. I have Item and Person entity class (persons and items as tables in database). This simplified the class to better show what I mean. Now i need to get list of items with login names using BeanListHandler. To do this i added login property to Item, but this is ugly solution. Is something better to do that and use advantages of BeanListHandler?
public class Person {
private Long id;
private String login;
}
public class Item {
private Long id;
private String name;
// ... a lot more properties of item
private Long personId; // this is real column in "items" table
private String login; // UGLY (login is not in "items" table, only for BeanListHandler)
}
QueryRunner q = new QueryRunner(getDataSource());
String sql = "select i.*, p.login from items i, persons p where p.id = i.personId";
List<Item> l = (List<Item>) q.query(sql, new BeanListHandler<Item>(Item.class));