For a project I want to get a LEFT JOIN working with criteria but I don"t get what I want.
I want to do this: Select Books FROM UserBooks LEFT JOIN Books ,....
But I don't get anything.
This is what I got at the moment:
#SuppressWarnings("unchecked")
public List<Books> getBooksByTitle(int userID, String Title) {
List<Books> books = null;
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
Criteria c = session.createCriteria(UserBooks.class, "userBooks");
c.setFetchMode("userBooks.book", FetchMode.JOIN);
c.createAlias("userBooks.book", "books");
c.setProjection(Projections.property( "books") ) ;
c.add(Restrictions.eq("userBooks.id", userID));
c.add(Restrictions.like("books.title", "%"+ Title + "%"));
books = c.list();
for (Books allBooks : (List<Books>)books) {
System.out.println(">>>>>" + allBooks.getTitle());
}
tx.rollback();
session.close();
factory.close();
return books;
}
never mind I got it :)
Criteria c = session.createCriteria(UserBooks.class, "userBooks");
c.createAlias("userBooks.book", "book");
c.setProjection(Projections.property("book"));
c.add(Restrictions.eq("userBooks.user.id", userID));
c.add(Restrictions.like("book.title", "%" + Title + "%"));
Related
I have an addEmployee webservice in the Spring 4 controller as shown in the code which is accepting emp_name and definition.
The insertion in the database table is happening using Hibernate 4 when the insertIntegertype method is called.
#RequestMapping(value="/addEmployee", method=RequestMethod.GET)
public String addEmployee
(
#RequestParam(value="name", defaultValue="") String emp_name,
#RequestParam(value="definition", defaultValue="") String definition
)
{ int emp_id = 0;
try {
EmpDao empDao = (EmpDao)context.getBean("empDao");
Emp empInsert = new Emp();
empInsert.setName(emp_name+" Attributes");
empInsert.setDefinition(definition);
empInsert.setOwnerName(owner_name_);
emp_id = empDao.insertIntegertype(empInsert);
}catch (Throwable th){
// some code here
}
}
Need to Include the following check:
As mentioned in the code while setting the name of an employee, I am also adding Attributes word in this line empInsert.setName(emp_name+" Attributes");
So, there can be a scenario when the same name is already present in the database and in that scenario, I would like to test
whether that name already exists in the database or not and then only proceed with the insert.
I am wondering is there a way in hibernate which can help me in figuring out this thing?
In case needed, my insertIntegertype hibernate method is defined as follows
public int insertIntegertype(Emp emp)
{
logger.debug("Starting EmpDaoImpl.insert() .....");
Session session = null;
Transaction tx = null;
boolean status = true;
int Emp_id = 0;
try {
session = sessionFactory.openSession();
tx = session.beginTransaction();
session.persist(emp);
tx.commit();
Emp_id = emp.getEmpId();
} catch(Exception ex) {
tx.rollback();
ex.printStackTrace();
status = false;
} finally {
session.close();
}
logger.debug("Completed EmpDaoImpl.insert() .....");
return Emp_id;
}
Approach :
Make a selection query from database with a like "Name%" if not exist make the Insertion
You have 3 options :
1-Using Native Query
Session session = sessionFactory.openSession();
session.beginTransaction();
String sql = "select First_Name FROM employee where First_Name like 'NAME%'"
NativeQuery query = session.createNativeQuery(sql);
List<Object[]> list = query.list();
if (list.isEmpty()) {
session.persist(emp);
}
2-Using HQL
Session session = sessionFactory.openSession();
session.beginTransaction();
Query query = session.createQuery("select u.name from Employee u where u.name like :Name");
List<Object[]> list= query.setParameter("Name", name+ "%").list();
if (list.isEmpty()) {
session.persist(emp);
}
3-Using CriteriaBuilder
Session session = sessionFactory.openSession();
session.beginTransaction();
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<String> criteria = builder.createQuery(String.class);
Root root = criteria.from(class);
Path <String> attribute = root.get(col);
criteria.select(attribute).where(builder.(attribute,NAME + "%"));
List<String> list = session.createQuery(criteria).getResultList();
if (list.isEmpty()) {
session.persist(emp);
}
You should query the database to check if an employee with such name exists. Then you can proceed with saving the employee according to your requirements
I got this exception,
org.hibernate.QueryException: expecting 'd', found 'f' [from
com.carre.model.ProductOrder where sessieid =
4f5bf46709a6886e764207a00ec0]
code form controller:
Cookie[] cookies = request.getCookies();
String sessieid = cookies[0].getValue();
model.addAttribute("listProductorderNieuw", this.productorderService.listProductorderNieuw(sessieid));
code form DAO:
#SuppressWarnings("unchecked")
#Override
public List<ProductOrder> listProductorderNieuw(String id) {
Session session = this.sessionFactory.getCurrentSession();
List<ProductOrder> productList = session.createQuery("from ProductOrder where sessieid = " + id).list();
for (ProductOrder p : productList) {
logger.info("ProductOrder List::" + p);
}
return productList;
}
I have no idea where this exception is coming from.
Someone can help?
Can you try using named paramner as follows
Query query = session.createQuery("from ProductOrder where sessieid = :id ");
query.setParameter("id", id);
List<ProductOrder> productList = query.list();
I have a spring MVC application with hibernate.I keep on getting the session closed error, when 10 or more users accessed the same page for reading the data or after fast subsequent requests.
Please help, I needed a crucial fix. It is affecting the customer.
I use the below code
try{
session = sessionFactory.openSession();
tx = session.getTransaction();
session.beginTransaction();
Map<Organization, List<Users>> comToUserLst
= new HashMap<Organization,List<Users>>();
String queryString = "FROM Users as usr Inner Join usr.organization
as org where org.id = :id";
Query query = session.createQuery(queryString);
query.setInteger("id", Integer.valueOf(id));
List<?> comLst = query.list();
Iterator<?> ite = comLst.iterator();
while (ite.hasNext()) {
Object[] objects = (Object[]) ite.next();
Users user = (Users) objects[0];
Organization Organization = (Organization) objects[1];
if (comToUserLst.containsKey(Organization)) {
List<Users> usrLst = new ArrayList<Users>();
usrLst.addAll(comToUserLst.get(Organization));
usrLst.add(user);
comToUserLst.put(Organization, usrLst);
} else {
List<Users> userLst = new ArrayList<Users>();
userLst.add(user);
comToUserLst.put(Organization, userLst);
}
}
} catch (HibernateException e) {
tx.rollback();
e.printStackTrace();
} finally {
tx.commit();
session.close();
}
return comToUserLst;
For update
#Transactional
public Account updateAccount(Account account, UserDetail userInfo) {
session = sessionFactory.getCurrentSession();
Account acct = null;
String queryString = "FROM Account where id = :acctId";
Query query = session.createQuery(queryString);
query.setLong("acctId", account.getId());
acct = (Account) query.uniqueResult();
acct.setName(account.getName());
acct.setPhone(account.getPhone());
acct.setRating(account.getRating());
acct.setFax(account.getFax());
acct.setAccountNumber(account.getAccountNumber());
if (!ValidateUtil.isNullOrEmptyCheckStr(account.getParentAccount()
.getName())) {
acct.setParentAccount(account.getParentAccount());
}
acct.setWebsite(account.getWebsite());
acct.setType(account.getType());
acct.setIndustry(account.getIndustry());
acct.setNumberOfEmployees(account.getNumberOfEmployees());
acct.setDescription(account.getDescription());
acct.setAnnualRevenue(account.getAnnualRevenue());
acct.setEmail(account.getEmail());
acct.setBillingAddress(account.getBillingAddress());
acct.setShippingAddress(account.getShippingAddress());
Users user = new Users();
user.setId(userInfo.getUserId());
// modified details
acct.setModifiedBy(user);
acct.setModifiedDate(new Date());
//update use merge
session.merge(acct);
session.flush();
System.out.println("update Account" + acct.getId());
return acct;
}
Exception
org.hibernate.SessionException: Session is closed!
at org.hibernate.internal.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:133)
at org.hibernate.internal.SessionImpl.getTransactionCoordinator(SessionImpl.java:2069)
at org.hibernate.loader.Loader.doQuery(Loader.java:923)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)
at org.hibernate.loader.Loader.doList(Loader.java:2553)
at org.hibernate.loader.Loader.doList(Loader.java:2539)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2369)
at org.hibernate.loader.Loader.list(Loader.java:2364)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:496)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:387)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:231)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1264)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)
at com.oi.service.setup.OrganizationService.getOrgToUserLst(OrganizationService.java:311)
at com.oi.service.setup.OrganizationService$$FastClassBySpringCGLIB$$84e99831.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
In Spring it might need to look like this:
#Autowired
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
#Transactional
public Map getOrgToUserLst() {
Map<Organization, List<Users>> comToUserLst = new HashMap<Organization,List<Users>>();
String queryString = "FROM Users as usr Inner Join usr.organization as org where org.id = :id";
List<?> comLst = this.sessionFactory.getCurrentSession()
.createQuery(queryString)
.setParameter(0, Integer.valueOf(id))
.list();
Iterator<?> ite = comLst.iterator();
while (ite.hasNext()) {
Object[] objects = (Object[]) ite.next();
Users user = (Users) objects[0];
Organization Organization = (Organization) objects[1];
if (comToUserLst.containsKey(Organization)) {
List<Users> usrLst = new ArrayList<Users>();
usrLst.addAll(comToUserLst.get(Organization));
usrLst.add(user);
comToUserLst.put(Organization, usrLst);
} else {
List<Users> userLst = new ArrayList<Users>();
userLst.add(user);
comToUserLst.put(Organization, userLst);
}
}
return comToUserLst;
}
#Transactional
public void saveOrganization(Organization org) {
this.sessionFactory.getCurrentSession().persist(org);
}
#Transactional
public void updateOrganization(Organization org) {
this.sessionFactory.getCurrentSession().merge(org);
}
#Transactional
public void deleteOrganization(Organization org) {
getCurrentSession().delete(org);
}
#Transactional
public void deleteOrganizationById(long id) {
final Organization org = this.getCurrentSession().get(Organization, id);
this.getCurrentSession().delete(org);
}
FOR UPDATE
// Get an account object by ID
public Account getAccount(long id) {
session = sessionFactory.getCurrentSession();
String queryString = "FROM Account where id = :acctId";
Query query = session.createQuery(queryString);
query.setLong("acctId", id);
return (Account) query.uniqueResult();
}
// Set account object's attributes
public Account updateAccount(Account acct, UserDetail userInfo) {
acct.setName(account.getName());
acct.setPhone(account.getPhone());
acct.setRating(account.getRating());
acct.setFax(account.getFax());
acct.setAccountNumber(account.getAccountNumber());
if (!ValidateUtil.isNullOrEmptyCheckStr(account.getParentAccount()
.getName())) {
acct.setParentAccount(account.getParentAccount());
}
acct.setWebsite(account.getWebsite());
acct.setType(account.getType());
acct.setIndustry(account.getIndustry());
acct.setNumberOfEmployees(account.getNumberOfEmployees());
acct.setDescription(account.getDescription());
acct.setAnnualRevenue(account.getAnnualRevenue());
acct.setEmail(account.getEmail());
acct.setBillingAddress(account.getBillingAddress());
acct.setShippingAddress(account.getShippingAddress());
Users user = new Users();
user.setId(userInfo.getUserId());
// modified details
account.setModifiedBy(user);
account.setModifiedDate(new Date());
updateAccount(acct);
}
// Update the account object in the database. Here transaction is necessary
#Transactional
private Account updateAccount(Account acct) {
session = sessionFactory.getCurrentSession();
//update use merge
System.out.println("update Account" + acct.getId());
return session.merge(acct);
}
// This is for testing
public void testUpdate(long id, UserDetail userInfo) {
Account acc = getAccount(id);
updateAccount(acct, userInfo);
}
Reference
How are you fetching your sessions SessionFactory.openSession() or SessionFactory.getCurentSession()? Keep in mind that hibernate sessions are not thread safe, hence you require one session per request response cycle.
You should use sessionFactory.getCurrentSession() in place of openSession(). This will maintain the opening and closing sessions automatically.
Also you should use Spring's Transaction using #Transactional with you method. Spring's Transaction management is more efficient than the Hibernate's getCurrentTransaction()
How can I get data from two linked tables (one-to-many: one User and many Results) by value 'ispassed' (boolean) using Ctriteria API?
private List<?> winners;
try {
SessionFactory factory = HibernateUtil.getSessionFactory();
Session hSession = factory.openSession();
Transaction tx = null;
try {
tx = hSession.beginTransaction();
winners = hSession.createSQLQuery("select * from usertable u, resulttable r where u.id = r.id where r.ispassed = true").list();
tx.commit();
} catch (Exception e) {
if (tx != null)
tx.rollback();
} finally {
hSession.close();
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(winners.size()); // an exception
You can use HQL:
from usertable u, resulttable r where u.id = r.id
where r.ispassed = 1
This will return a list of [User,result] arrays.
Change you code like:
private List<?> winners;
try {
SessionFactory factory = HibernateUtil.getSessionFactory();
Session hSession = factory.openSession();
Transaction tx = null;
try {
tx = hSession.beginTransaction();
winners = hSession.createSQLQuery("from usertable u, resulttable r where u.id = r.id and r.ispassed = true").list();
tx.commit();
} catch (Exception e) {
if (tx != null)
tx.rollback();
} finally {
hSession.close();
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(winners.size());
EDIT:
CriteriaBuilder b = em.getCriteriaBuilder();
CriteriaQuery<Tuple> c = b.createTupleQuery();
Root<EntityX> entityXRoot= c.from(EntityX.class);
Root<EntityY> entityYRoot = c.from(EntityY.class);
List<Predicate> predicates = new ArrayList<>();
//Here you need to add the predicates you need
List<Predicate> andPredicates = new ArrayList<>();
andPredicates.add(b.equal(entityXRoot.get("id"), entityYRoot.get("id")));
andPredicates.add(b.and(predicates.toArray(new Predicate[0])));
c.multiselect(entityXRoot, entityYRoot);
c.where(andPredicates.toArray(new Predicate[0]));
TypedQuery<Tuple> q = em.createQuery(criteria);
List<Tuple> result = q.getResultList();
You can create your entity class like below
#Entity
#Table(name="RESULTS")
public class Results implements Serializable {
#Id
#GeneratedValue()
#Column(name="ID")
private Long id;
#ManyToOne
#JoinColumn(name="USER_ID")
private User userId;
#Column(name = "IS_PASSED")
private Boolean ispassed;
other property
... getter() setter()
}
#Entity
#Table(name="USER")
public class User implements Serializable {
#Id
#GeneratedValue()
#Column(name="ID")
private Long id;
#OneToMany(mappedBy = "userId",cascade=CascadeType.ALL)
private Set<Results> resultsSet;
other property
... getter() setter()
}
And in your hibernate.cfg.xml file if set below property
<property name="hibernate.query.substitutions">true 1, false 0</property>
Execute below HQL query
String sql = "from User as user "
+ "inner join user.resultsSet"
+ "where resultsSet.ispassed= true";
Query query = getCurrentSession().createQuery(sql);
List<User> UserList = query.list();
above is how you can get List of User, now you need to Iterate User list and use getter method get all results.
I'm selecting records in JAVA with JPA and playframework like this:
EntityManager em = JPA.em();
List<News> resultUrl = News.find("link", url).fetch();
if (resultUrl.isEmpty()) { //check if it is exist
}
But i want to select records with two condition, like this:
where link='url' and name='joe'
How can i do this?
Thanks for helping.
Best wishes.
Use:
Query q = em.createQuery("FROM News n WHERE n.link=:url and n.name=:name");
q.setParameter("url", "url").setParameter("name", "joe");
List<News> resultUrl = q.getResultList();
...
One way to do it with Play is
List<News> resultUrl = News.find("byLinkAndName", url, "joe").fetch();
if (resultUrl.isEmpty()) { //check if it is exist
}
Another:
List<News> resultUrl = News.find("link = ? and name = ?", url, "joe").fetch();
if (resultUrl.isEmpty()) { //check if it is exist
}
My proposal is to define a named query:
#Entity
#NamedQueries({
#NamedQuery(name = News.FIND_BY_URL_AND_NAME, query = "Select n FROM News as n WHERE n.url=:" + News.PARAM_URL + " AND n.name=:" + News.PARAM_NAME)
})
public class News {
public static final String FIND_BY_URL_AND_NAME = "News.findByUrlAndName";
public static final String PARAM_URL = "url";
public static final String PARAM_NAME = "name";
//CONTINUE
}
Then you call it like that:
Query query = em.createNamedQuery(News.FIND_BY_URL_AND_NAME);
query.setParameter(News.PARAM_URL, "url");
query.setParameter(News.PARAM_NAME, "name");
List<News> news = query.getResultList();
Get a look at CriteriaBuilder, CriteriaQuery and Predicate :
EntityManager em = JPA.em();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<T> criteriaQuery = cb.createQuery(News.class);
Root<T> root = criteriaQuery.from(News.class);
criteriaQuery.select(root);
List<Predicate> ps = new ArrayList<Predicate>();
ps.add(sb.equal(root.get("link", url));
ps.add(sb.equal(root.get("name", "joe"));
criteriaQuery.where(cb.and(ps.toArray(new Predicate[0])));
List<News> resultUrl = em.createQuery(criteriaQuery).getResultList();
Regards