Persistence.getPersistenceUtil [message #827345] |
Fri, 23 March 2012 03:45  |
Eclipse User |
|
|
|
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 #827664 is a reply to message #827558] |
Fri, 23 March 2012 12:20   |
Eclipse User |
|
|
|
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 14:37  |
Eclipse User |
|
|
|
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
|
|
|
Powered by
FUDForum. Page generated in 0.28884 seconds