join 2 columns and get only matched vlaues in hibernate - sql

I have 2 tables:
course: id, name
student: id, course_id, age
Tables are tied with oneToMany annotation
I am trying to write a hibernate query against course that will return courses and students that are x years 0ld. That's my attempt:
SELECT c from Course c RIGHT JOIN c.student p ON p.age = 20
It returns a course that has at least one student that is 20. when there are more students that are not 20, they are included in the result set as well. Is there a way to restrict the joined table values?
Edit:
These are my entities:
public class Course implements Serializable {
private static final long serialVersionUID = 646349111982187812L;
#Id
#Column(name = "id", unique=true, nullable=false)
private String id;
#Column(name = "name", unique=false, nullable=false)
private String name;
#OneToMany(cascade={CascadeType.ALL, CascadeType.REMOVE},
fetch=FetchType.LAZY, mappedBy = "Course")
#OrderBy("age")
#JsonManagedReference
private Set<Student> students;
getters and setters ...
}
public class Student implements Serializable {
private static final long serialVersionUID = 646349221337813L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#Column(name = "age", unique=false, nullable=true)
private String age;
#ManyToOne()
#JsonBackReference
private Course course;
}
I am trying to use this method from my crudrepository to get the expected result:
#Query(?????)
public List<Course> findCoursesWithAdults(#Param("age")int age)

it sounds like you want an inner join instead of a right join
SQL
SELECT distinct c.* from Course c inner join Student p on (c.id=p.course_id and p.age=20)
or with hql
select distinct c from Course c inner join c.student p with p.age=20
in order to get the information you are requesting, you will either have to limit the Set in Course using #Where ("age = 20" ) or simply deal with a list of students instead of courses.
select p from Course c inner join c.student p with p.age=20
you can reference the attached course object through any one of the getCourse methods on the student objects. You could also use "group by" here to help you order things.
or you can use a filter within the hibernate Entity...
#Transient
public List<Students> getStudents(Integer age){
List<Students> tmp = new ArrayList<>();
for(Student s: getStudents())
if(s.getAge().equals(age))tmp.add(s);
return tmp;
}
one more edit and im done... there is another way to do this that i didnt mention..
a select wrapper query.
create a wrapper object for your course and student
public class CourseWrapper(){
private Course c;
private List<Students> p = new ArrayList<>();
....constructor ... getters ...setters
}
select new CourseWrapper(c, students)
from Course c
left outer join c.students p with p.age=20'
more info here

SELECT c.name, s.id
FROM course c
INNER JOIN student s ON c.id = s.course_id
WHERE s.age = 20

Related

How to search two tables sharing a foreign key (I think I'm asking this right....)?

Dog entity
#Entity(tableName = "dog_table")
public class DogEntity {
private int mId;
private String mName, mBreed;
etc..
}
Toy entity
#Entity(tableName = "toy_table")
public class ToyEntity {
private int mId;
private String mName, mBrand;
etc..
}
DogAndToy join table entity
#Entity(tableName = "dog_and_toy_join_table",
primaryKeys = {"mDogID", "mToyId"},
foreignKeys = {
#ForeignKey(
entity = DogEntity.class,
parentColumns = "mId",
childColumns = "mDogID",
onDelete = ForeignKey.CASCADE,
onUpdate = ForeignKey.CASCADE
),
#ForeignKey(
entity = ToyEntity.class,
parentColumns = "mId",
childColumns = "mToyId",
onDelete = ForeignKey.CASCADE,
onUpdate = ForeignKey.CASCADE
)
},
indices = {#Index("mDogID"), #Index("mToyId")}
)
public class DogAndToyJoinEntity{
private final int mDogID, mToyId;
public DogAndToyJoinEntity(int mDogID, int mToyId) {
this.mDogID = mDogID;
this.mToyId = mToyId;
}
etc..
}
DogAndToy data class
public class DogAndToy {
#Embedded
public Dog mDog;
#Relation(
parentColumn = "mId",
entityColumn = "mId",
entity = ToyEntity.class,
associateBy =
#Junction(
value = DogAndToyJoinEntity.class,
parentColumn = "mDogId",
entityColumn = "mToyId"
)
)
public List<ToyEntity> toyList;
}
notes: All dogs can have multiple toys, and toys can be associated with multiple dogs. Dog & Toy entities don't share any fields (eg - Dog doesn't have toyId, etc)
I've been trying for a few days to wrap my head around
how to query/get all dogs associated with one Toy (by name)
I use the DogAndToy data class for display purposes in my RecyclerView.
JOIN and INNER JOIN queries are baffling to me and I've been trying multiple variations but keep ending up with zero search results. Here's my most recent try:
#Transaction
#Query("SELECT dog_table.* FROM dog_table" +
"INNER JOIN dog_and_toy_join_table ON dog_table.mId = dog_and_toy_join_table.mDogId" +
"INNER JOIN toy_table ON toy_table.mId = dog_and_toy_join_table.mToyId " +
"WHERE toy_table.mName LIKE :query")
LiveData<List<DogAndToy>> findDogsByToyName(String query);
Can anyone suggest a step-by-step description of these queries in Android Room? Any of the JOIN articles/examples I find here or anywhere on the internets don't have a "join" (foreign key) reference...
Am I even trying this in the right manner?
update: to clarify, I have FTS tables and my "basic" searches work fine (eg - search by name, etc)
Replace :toyName with the variable
SELECT d.mName FROM dog_table AS d
WHERE d.mId IN (
SELECT j.mDogID FROM dog_and_toy_join_table AS j
WHERE j.mToyId = (
SELECT t.mId FROM toy_table AS t
WHERE t.mName = :toyName));
EDIT
TBH, no idea why it only selects one row. Maybe someone else can answer it.
It the mean time take this:
select d.mName
from dog_table d
INNER join dog_and_toy_join_table dt
on d.mid = dt.mDogID
INNER JOIN toy_table t
ON dt.mToyId = t.mId
WHERE t.mName = 'toy1'

Spring Data JPA Query for inner join table throwing error

Spring DATA JPA question... I am trying to write a query to access the data in my sql join table.
I have my join table set up as follows:
#Entity
#Table(name="WritingCompany")
public class WritingCompany {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "companyId")
private Long id;
// private String companyName;
#ManyToMany
#JoinTable(name = "Join-WritingCo-Carrier",
joinColumns = #JoinColumn(name="writingCo"),
inverseJoinColumns = #JoinColumn(name="carrier")
)
private Set<CarrierAppointment> carriers;
//...getters and setters
}
public class CarrierAppointment {
// ...
#ManyToMany(
cascade=CascadeType.ALL,
mappedBy = "companyName"
)
private Set<WritingCompany> companies;
public Set<WritingCompany> getCompanies() {
return companies;
}
public void setCompanies(Set<WritingCompany> companies) {
this.companies = companies;
}
//...
}
I am unsure which class repository I need to write the query for... I am trying to find all the writingCo names from the join table that all have the same carrier id that matches one specific carrier.
The Join Table is set up through writing company but I feel like it should be accessed through CarrierAppointment Repository since I am matching carrier Id's.
This is what I've tried in the CarrierAppointmentrepository and it throws this error (unexpected token: Join near line 1, column 94):
#Query("Select companyName FROM WritingCompany INNER JOIN CarrierAppointment ON Join-WritingCo-Carrier.carrier= CarrierAppointment.CarrierAppointmentId")
List<CarrierAppointment> findCarrierAppoinmentFromJoin(CarrierAppointment carrier);
I've also tried:
#Query("SELECT writingCo FROM Join-WritingCo-Carrier WHERE carrier= CarrierAppointment.CarrierAppointmentId")
List<CarrierAppointment> findCarrierAppoinmentFromJoin(CarrierAppointment carrier);
Then I tried this in the writingCompanyRepository which also throws a similar error:
#Query("Select companyName FROM WritingCompany INNER JOIN CarrierAppointment ON Join-WritingCo-Carrier.carrier= CarrierAppointment.CarrierAppointmentId")
List<WritingCompany> findAllWithDescriptionQuery(CarrierAppointment carrier);
I am having a hard time understanding what this query is saying. Do I ever need to access the columns from the sql join table, or am I just querying around the join table by asking for each class that is making up the join columns in the join table? What is the right part of the statement, after INNER JOIN stating ? Could someone please provide a deeper explanation of why the query is written so I can figure out why it's not working? I've been reading a lot of inner join examples and just can't seem to figure it out.

Hibernate SQL query matching more than one members of an ElementCollection

I have Pojo mapped with JPA annotations like this
#Entity
#Table(name = "record")
public class SearchRecord {
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE)
private int id;
private String vidName;
#ElementCollection
private List<String> videoLabels = new ArrayList<String>();
and would like to run a Hibernate query that filters out all SearchRecords that match/contain 1..n videoLabels. (only objects that match all of the videoLabels)
I was able to search for SearchRecords that match a single label by running the following query:
String labelCar = "Car";
String labelPerson = "Person";
TypedQuery<SearchRecord> query = em.createQuery("SELECT b FROM SearchRecord b JOIN b.videoLabels l WHERE l = :param1",SearchRecord.class);
query.setParameter("param1", labelCar);
List<SearchRecord> results = query.getResultList();
But how can I execute a query filtering out all SearchResults matching Car and Person?
Thanks for your support
I was able to solve the problem with the following query
SELECT DISTINCT a FROM SearchRecord a JOIN a.labels b JOIN a.labels c WHERE b.name = 'Car' and c.name = 'Person'

deleting entries with JPA and subqueries

I just wrote an sql query :
DELETE FROM basisgegevens.gm_persoonburgstaat pbs
WHERE (pbs.ingangsdatum, pbs.id_persoon) in (
SELECT pbs2.ingangsdatum, pbs2.id_persoon
FROM basisgegevens.gm_persoonburgstaat pbs2
WHERE pbs2.ingangsdatum = pbs.ingangsdatum
AND pbs2.id_persoon = :persoonID
AND pbs2.id_persoonburgerlijkestaat > pbs.id_persoonburgerlijkestaat);
I need to rewrite it to JPQL, but am getting stuck with the subquery refrencing the outer query.
public class PersoonBurgerlijkeStaatEntity {
#Column(name = "id_persoonburgerlijkestaat"
private Long identifier;
private Date ingangsdatum;
#ManyToOne
#JoinColumn(name = "id_persoon", referencedColumnName = "id_persoon", nullable = false)
private PersoonEntity persoon;
}
The persoon entity has an identifier
Can someone help me rewrite this?
Thanks
Not sure about this but give a try.
DELETE FROM persoonburgstaat person where (person.ingangsdatum, person identifier) in
(select p.ingangsdatum, p.identifier from persoonburgstaat p
left join p.persoon per where per.id_persoon = :persoonID
AND per.id_persoonburgerlijkestaa > p.identifier)
the left join will make the outer query
But to be more sure post PersoonEntity entity as I think " id_persoonburgerlijkestaa " is the name of the column not the property and query will fail based on that.

add Nhibernate Projections

Have Employee: BaseEntity class with
public virtual BaseEntity Department {get;set;}
public virtual BaseEntity Position {get;set;}
and BaseEntity class:
public virtual long Id {get;set;}
public virtual long Name {get;set;}
How can I map my entities and /create select like this
SELECT DepartmentTable.nvarchar1 as Department, PositionTable.nvarchar1 as Position, COUNT(*) as N
FROM AllUserData EmployeeTable
left outer join AllUserData PositionTable on EmployeeTable.int2=PositionTable.tp_ID
left outer join AllUserData DepartmentTable on EmployeeTable.int3=DepartmentTable.tp_ID
WHERE EmployeeTable.tp_ListId = #p0 and PositionTable.tp_ListId = #p2 and DepartmentTable.tp_ListId = #p1
GROUP BY DepartmentTable.nvarchar1, PositionTable.nvarchar1;
I tried like this:
var criteria = session.CreateCriteria<Entities.Employee>();
criteria.CreateAlias("Position", "PositionTable", JoinType.LeftOuterJoin);
criteria.Add(Restrictions.Or(Restrictions.Eq("PositionTable.ListId", Program.PositionListGuid), Restrictions.IsNull("PositionTable.Id")));
criteria.CreateAlias("Department", "DepartmentTable", JoinType.LeftOuterJoin);
criteria.Add(Restrictions.Or(Restrictions.Eq("DepartmentTable.ListId", Program.DepartmentListGuid), Restrictions.IsNull("DepartmentTable.Id")));
criteria.Add(Restrictions.Eq("ListId", Program.EmployeeListGuid));
var projectionList = Projections.ProjectionList();
projectionList.Add(Projections.RowCount());
projectionList.Add(Projections.GroupProperty("Department"), "AliasedId");
criteria.SetProjection(projectionList);
criteria.SetResultTransformer(Transformers.AliasToBean(typeof(BaseEntity)));
var list = criteria.List<BaseEntity>();
// "More than one row with the given identifier was found" exception raised here
I'am confused, should I somehow use joined table alias for Projections.GroupProperty, rework my mapping/Entity to use only primitive types (string, int) or any other approach?
Figured out:
Add int Number to BaseEntity and string DepartmentName, string PositionName to Employee. Now I can use Projections to map Sql grouped by result to this properties:
var pl = Projections.ProjectionList()
.Add(Projections.RowCount(), GetEntityProperty(typeof (BaseEntity), "Number"))
.Add(Projections.GroupProperty("DepartmentTable.Title"), "PositionName")
.Add(Projections.GroupProperty("PositionTable.Title"), "DepartmentName");
ICriteria criteria = session.CreateCriteria<Employee>()
.CreateAlias("Position", "PositionTable", JoinType.LeftOuterJoin)
.Add(Restrictions.Eq("PositionTable.ListId", Program.PositionListGuid))
.CreateAlias("Department", "DepartmentTable", JoinType.LeftOuterJoin)
.Add(Restrictions.Eq("DepartmentTable.ListId",Program.DepartmentListGuid))
.Add(Restrictions.Eq("ListId", Program.EmployeeListGuid))
.SetProjection(pl)
.SetResultTransformer(Transformers.AliasToBean<BaseEntity>());
var list = criteria.List<BaseListEntity>().ToList();