Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Persistence.getPersistenceUtil(to find if associative entities are loaded)
Persistence.getPersistenceUtil [message #827345] Fri, 23 March 2012 07:45 Go to next message
Mitesh Pandey is currently offline Mitesh PandeyFriend
Messages: 7
Registered: March 2012
Junior Member
Hi,

I have an entity like below.The example is a generic one and not the actual code

@entity
public class company
{
@Id
private int companyId
@onetoMany
private list<employee> employee;
}

@entity
public class employee
{

}

My requirement is that for a given companyId , I should be able to find out that employee entity is null or not null

I googled and found that there exist a method Persistence.getPersistenceUtil helps in finding out the same requirement.

I am using JPA in JAVA SE environment with eclipselink 2.3.2 as JPA provider. Problem 1 : getPersistenceUtil method does not exist when i am trying to call it on persistence class.

Problem 2 : What are the different ways to find that for the entity, which are the associated entities that exist
Re: Persistence.getPersistenceUtil [message #827558 is a reply to message #827345] Fri, 23 March 2012 13:53 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
getPersistenceUtil is a JPA method added in JPA 2.0. So you will need to be using the 2.0 javax.persistence jar to be able to access the method.

The class returned from getPersistenceUtil used to determine if a lazy loaded attribute has been loaded or not, but you mention you want to check if the entity is null. Company has a list of employees, which should never be null if read in (it might be empty), so can you describe a bit more what you are after?
Re: Persistence.getPersistenceUtil [message #827664 is a reply to message #827558] Fri, 23 March 2012 16:20 Go to previous messageGo to next message
Mitesh Pandey is currently offline Mitesh PandeyFriend
Messages: 7
Registered: March 2012
Junior Member
Hi Chris,

Yes indeed, I was using javax.persistence_1. So after putting javax_persistence_2 , i have accessed to Persistence.getPersistenceUtil(). Thanks a lot.

For problem no 2, my exact requirement is as below

@entity
public class company
{
@Id
private int companyId
private String companyName;
@onetoMany
private list<employee> employee;
@onetoMany
private List<SubsidaryCompany> subsidaryCompany;
@onetoMany
private List<Department> department;
@onetoMany
private List<LawFirm> lawFirmHired;
}

@entity
public class employee
{

}
@entity
pubic class SubsidaryCompany
{
}

@entity
public class Department
{
}

@entity
public class LawFirm
{
}

Below is the sample data for example

Company
--------------------------
companyId companyName
1 walmart
2 tesco

Employee
-----------------------------------
EmployeeId EmployeeName companyId
1 Bob 1
2 Chan 1

SubsidaryCompany
------------------------------
SubCompanyId CompanyId
1 1
2 1


Department
---------------------------
DepartmentId CompanyId
1 1
2 1

LawFirm
----------------------
LawFirmId CompanyId
1 1
2 1
3 2


So for Walmart, it has two employee , 2 subsidary company, 2 department and 2 lawfirm and for tesco it has no employee, no subsidary, no department but 1 lawfirm hired

If i query company entity with companyId 2 (tesco), it will also load associated entity like employee, subsidaryCompany, department and lawfirm but except lawFirmHired all other attributes would be null.

So if I want to check what are the associated entities that are not empty or not null, I have to every attribute against null

In my actual scenario, an entity like company has more than 30 relationship.

I need a way to know what are the associated entities which are not null, rather than checking every attribute for null value
Re: Persistence.getPersistenceUtil [message #827736 is a reply to message #827664] Fri, 23 March 2012 18:37 Go to previous message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
Sorry, i'm not sure I follow.
If you want to check if the collection is null or empty, wouldn't you just call company.getEmployee().isEmpty()?
Are you asking for all Ccmpany entities that have employees? If so:
em.createQuery("SELECT company FROM Company company WHERE company.employee IS NOT EMPTY").getResultList();
will get you only companies with employee references.

Best Regards,
Chris
Previous Topic:Some Discriminator Values cause problem, while others do not
Next Topic:EclipseLink JPA: Composite Persistence Units Example
Goto Forum:
  


Current Time: Thu Apr 18 02:33:40 GMT 2024

Powered by FUDForum. Page generated in 0.02323 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top