I have a table Loan application whose pojo is
class LoanApplication{
int id;
int loanNo;
...
}
I have another pojo
class LoanFlow{
int id;
int loanId;
date reviewDate;
...
}
Here loanId of loanFlow is the foreign key mapped to the id of LoanApplication.
I have to fetch all the loan applications with the reviewDate.
I am trying to write a criteria like:
Criteria criteria = getSession().createCriteria(LoanApplication.class);
criteria.add(Restrictions.eq("id", someId));
How can I fetch the reviewDate also from LoanFlow with this criteria.
Criteria criteria = getSession().createCriteria(LoanApplication.class, "loApp");
criteria.createAlias("loApp.loanFlow", "flow");
criteria.add(Restrictions.eq("flow.id", 1));
You can directlly use HQL also.
using createQuery()
You can do it with subqueries, if there isn't ManyToOne or OneToMany annotations to the relationship between entities:
DetachedCriteria subQuery = DetachedCriteria.forClass(LoanFlow.class, "loanFlow");
/*Here there is a between, but you can change it to your necessity*/
subQuery.add(Restrictions.between("loanFlow.reviewDate", dateBegin, dateEnd));
subQuery.setProjection(Projections.property("loanFlow.loanId"));
Criteria criteria = getSession().createCriteria(LoanApplication.class, "loanApplication");
Subqueries.propertyIn("loanApplication.id", subQuery);
List<LoanApplication> list = criteria.list();
Related
I have an entity:
#Entity
#Table(name ="cats")
public class Cat {
#Id
#Column(name="name")
private String name;
#Column(name="age")
private int age;
#Column(name="color")
private String color;
#Column(name="weight")
private int weigth;
..
}
1. I need to delete it from database using EntityManager:
#Override
public void delete(Cat cat) {
entityManager.remove(cat);
}
Problem: I have a Map<String, Cat> which contains all this elements. I get it by name from map IllegalArgumentException -> "Removing a detached instance com.entities.Cat#cats".
Question: How can i do it without getting from database by key?
2. I need to getList with limit and offset.
To get all the elements i can just use:
entityManager.createNativeQuery("SELECT name, age, color, weight FROM cats");
Without entityManager i used prepatedStatement with:
"SELECT name, age, color, weight FROM cats LIMIT ?,?"
Question:
How can i do it using entityManager?
Do entityManager have something like preparedStatement?
With EntityManager you can use Query objects. It provides you with several different methods to build your queries, which you can see in the Docs.
From there, you can use a Query to perform a select or execute an update into the db.
Update example:
//:id is a parameter you can set
Query query = entityManager.createQuery("delete from Entity e where e.id = :id");
query = query.setParameter("id", id);
query.executeUpdate();
Select example (using TypedQuery which implements Query:
String sql = "select e from Entity e";
TypedQuery<Entity> query = entityManager.createQuery(sql, Entity.class);
System.out.println(query.getResultList());
You can determine limit and offset like this:
query = query.setFirstResult(offset);
query = query.setMaxResults(limit);
If you have an entity at hand you can (and should) delete it using your EntityManager with remove(). You're getting that error because your entity is detached - that is, your EntityManager isn't aware of its existence.
To "attach" entities to your manager you can use merge(). However, if said entity doesn't exist in the database it will be inserted, and if it exists but has different fields from your object it will be updated.
public void delete(Cat cat) {
if(!entityManager.contains(cat)) {
entityManager.merge(cat);
}
entityManager.remove(cat);
}
To insert entities for the first time you can also use persist(). For the differences between merge() and persist(), see this.
If you need to use EntityManager, then simply use reference:
entityManager.remove(entityManager.getReference(Cat.class, id));
This way the entity won't be fetched from db, but will be deleted.
Using query is also an option:
Query query = entityManager.createQuery("delete from Entity e where e = :entity");
query = query.setParameter("entity", entity);
query.executeUpdate();
You can create Query using EntityManager#createQuery. Then set parameters: firstResult and maxResults:
query.setFirstResult(10).setMaxResults(20);
This will take 20 entities starting from 10th.
i have two entity(employeeTo & restTo) there is one-to-many relationship
between them,i want to get employee restTo list only, but when i load the list each of object contain employee object.
employeeTO
#OneToMany(mappedBy = "employeeTO",cascade = CascadeType.ALL)
private List<RestTO> RestTOList=new ArrayList<>();
restTO
#ManyToOne(fetch = FetchType.LAZY)
private EmployeeTO employeeTO;
and you can see my code below:
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = session.beginTransaction();
Criteria criteria=session.createCriteria(RestTo.class);
criteria.setFetchMode("employeeTO",FetchMode.SELECT);
criteria.createAlias("employeeTO","alians_employee");
Criterion condition1= Restrictions.eq("alians_employee.emID",emid);
criteria.add(condition1);
criteria.add(Restrictions.between("startDate",date1,date2));
List<RestTO> restTOS=criteria.list();
transaction.commit();
session.close();
System.out.println(restTOS.get(0).getEmployeeTo.getName);
This instruction is working and show employee name
I want to not have access to employee object
i need load only RestTo object without employeeTo value.
how i can do it?
thanks.
Don't create an alias,it adss an inner join to your query. So instead of this
Criteria criteria=session.createCriteria(RestTo.class);
criteria.setFetchMode("employeeTO",FetchMode.SELECT);
criteria.createAlias("employeeTO","alians_employee");
Criterion condition1= Restrictions.eq("alians_employee.emID",emid);
Use this
Criteria criteria=session.createCriteria(RestTo.class);
//Default fetchmode is good,no need for that.
//criteria.setFetchMode("employeeTO",FetchMode.SELECT);
//criteria.createAlias("employeeTO","alians_employee");
Criterion condition1= Restrictions.eq("employeeTO.emID",emid);
Select * from Table A left join Table B on A.id=B.id and A.langCode='IN';
where 'IN' is input from user.
Table A and Table B has Mapping For Id but there is no Mapping with LangCode between the two as table B dosent have an column called langCode to map with , i want to write the following query using hibernate criteria without mapping langcode.
Table: Employee :
EMP_ID - primary key ,
NAME ,
CONTACT_DETAILS
Table:Employee_Lang:
EMP_ID- composite primary key,
LANG_CODE- composite primary key,
NAME
Actual Query:
Select * from Employee Emp left outer join Employee_Lang EmpLang on Emp.EMP_ID=EmpLang.EMP_ID AND EmpLang.LANG_CODE='IN'
I have mapped only the Emp_Id as primary key from both the tables in hibernate hence hibernate criteria will only apply a join on that
And not on LangCode.
Note:-I cant change hibernate mapping and can use only hibernate Criteria , as per the clients requirement, please help me on this one.
For example you have two models:
#Entity
class Employee {
#Id
private Long id;
#OneToOne
private EmployeeLang employeeLang;
...
}
#Entity
class EmployeeLang {
#Id
private Long id;
private String langCode;
#OneToOne
private Employee employee;
...
}
You should have some mapping between them and then you can do next criteria:
Criteria criteria = session.createCriteria(Employee.class);
criteria.createAlias("employeeLang", "empLang"); // inner join with EmployeeLang
criteria.add(Restrictions.eq("empLang.langCode", "in");
List<Employee> employees = criteria.list();
I have two tables in my PostgreSQL database:
CREATE TABLE tableOne (id int, name varchar(10), address varchar(20))
CREATE TABLE tableTwo (id int, info text, addresses varchar(20)[])
now I want to create a join as follows:
SELECT * FROM tableOne JOIN tableTwo ON address = ANY(addresses)
I tried to achieve this using Hibernate - class TableOne:
#Entity
#Table(name = "tableOne")
class TableOne {
private int id;
private TableTwo tableTwo;
private String address;
#Id
#Column(name = "id")
public getId() { return id; }
#ManyToOne
#JoinFormula(value = "address = any(tableTwo.addresses)",
referencedColumnName = "addresses")
public TableTwo getTableTwo(){
return tableTwo;
}
// Setters follow here
}
But Hibernate keeps generating queries with non-sense JOIN clauses, like:
... JOIN tableTwo ON _this.address = any(tableTwo.addresses) = tableTwo.addresses
How do I tell Hibernate using annotations to format my join query correctly? Unfortunately, our project must be restricted only to the Criteria API.
EDIT:
After suggestion from ashokhein in the comments below, I annotated the method getTableTwo() with just #ManyToOne - and now I would like to do the join using Criteria API, presumably with createAlias(associationPath,alias,joinType,withClause) method where withClause would be my ON clause in the join.
But Im not sure what to put as associationPath and alias parameters.
Any hints?
To support PostgreSQL array you need a custom Hibernate Type. Having a dedicated user type will allow you to run native SQL queries to make use of the type:
String[] values = ...
Type arrayType = new CustomType(new ArrayUserType());
query.setParameter("value", values, arrayType);
HQL supports ANY/SOME syntax but only for sub-queries. In your case you'll need a native query to use the PostgreSQL specific ANY clause against array values.
You can try Named Query.
#NamedQuery(name="JOINQUERY", query="SELECT one FROM tableOne one JOIN tableTwo two ON one.address = :address" )
#Entity
class TableOne{......
Retrieving part is:
TypedQuery<TableOne> q = em.createNamedQuery("query", TableOne.class);
q.setParameter("address", "Mumbai");
for (TableOne t : q.getResultList())
System.out.println(t.address);
You might need to do some permutations on the query
So after a lot of time searching for the right answer, the only real solution that works for us is creating a view:
CREATE VIEW TableA_view AS SELECT TableOne.*,TableTwo.id FROM TableA JOIN TableTwo ON TableOne.address = ANY(TableTwo.addresses)
and mapping it to an entity TableOne instead of the original table.
This was the only solution for us besides, of course, using a named query, which was a no-go as we needed to stick to the Criteria API.
As #ericbn has mentioned in the comments this is really an example where ORM gets really annoying. I would never expect that custom join clause like this is not possible to do in Hibernate.
#JoinFormula should contain SQL instead of HQL.
https://docs.jboss.org/hibernate/orm/4.2/javadocs/org/hibernate/annotations/JoinFormula.html
JSF application with hibernate
Is there a way to use a join to filter the results returned by criteria list?
Example: i have 2 tables. orders and customers.
#Entity(name = "order")
public class Order
{
#Id
private int id;
private String billingCustomerId;
private String shippingCustomerId;
private Date orderDate;
....
}
#Entity(name = "customer")
public class Customer
{
#Id
private String id;
private String name;
private String emailAddress
....
}
I need to return all orders for customers that are missing an email address and all orders that the order.billingCustomerId = null and order.shippingCustomerId = null.
The customer could match on the billingCustomerId or shippingCustomerId.
The SQL I would use
select o.* from order as o
LEFT join customer as c1 on o.billingCustomerId = c1.id
LEFT join customer as c2 on o.shippingCustomerId= c2.id
where (o.billingCustomerId is null and o.shippingCustomerId is null) or
(o.billingCustomerId is not null and c1.emailAddress is null) or
(o.shippingCustomerIdis not null and c2.emailAddress is null)
Hibernate Criteria
Criteria criteria1 = session.createCriteria(Order.class);
criteria.add(Restrictions.and(Restrictions.isNull("billingCustomerId"),
Restrictions.isNull("shippingCustomerId"));
List<Order> = criteria.list();
This will return the list of orders that billing /shipping customer = null.
How can i change the criteria to also include the orders for customers with missing email addresses?
Disjunction disjunciton = Restrictions.disjunction();
Criteria criteria = session.createCriteria(Order.class);
disjunciton.add(Restrictions.and(Restrictions.isNull("billingCustomerId"),
Restrictions.isNull("shippingCustomerId")));
disjunciton.add(...
...)
criteria.add(disjunciton);
List<Order> = criteria.list();
I have not been able to find examples of joining on a column, but only where the table have a common key.
I asked this question: Hibernate trouble getting composite key to work and discovered Hibernate can only create a join on columns that were created by relating 2 objects. I am going to add more to my answer to give more useful information but the best alternative you your case is to do a Session.createSQLQuery() using the query you showed above. Then before running the query put Query.addEntity(Order.class).addEntity(Customer.class). As long as your query returns the correct rows to fill out the Java objects correctly, Hibernate can populate them automatically. If that doesn't work you can still retrieve the data and populate it manually yourself.