How to write select named query in jpa for EmbeddedId? - sql

I have a JPA entity called Parent and inside that there is embeddedPrimary key as Child
#Entity
#Table(name = "PARENT")
#NamedQuery(?????)
public class Parent implements Serializable {
#EmbeddedId
private ChildPK child;
}
and
#Embeddable
public class ChildPK implements Serializable {
#Column(name = "DEALERID")
private String dealerId;
#Column(name = "BRANDID")
private Long brandId;
..
}
How can I write named Query in Parent class so that i can perform select on dealerId and brandId from ChildPk.

SELECT p FROM Parent p WHERE p.child.dealerId = ? and p.child.brandId=?

Related

How to use QueryDSL with SINGLE_TABLE?

I am using JPA with QueryDSL.
Here are my entities with single table inheritance strategy.
#Entity
#Inheritance(strategy = InheritanceType.SINGLE_TABLE)
#DiscriminatorColumn(name = "type")
public abstract class Parent {
private int parentId;
private LocalDateTime createdAt;
...
}
#Entity
#DiscriminatorValue("a")
public class A extends Parent {
...
}
#Entity
#DiscriminatorValue("b")
public class B extends Parent {
...
}
#Entity
#DiscriminatorValue("c")
public class C extends Parent {
...
}
I have to write a query with QueryDSL that inquires A and B in order of createdAt value.
If I can't do this with QueryDSL what can I do?

ORM: EclipseLink AssociationOverride "Persistent type of override attribute cannot be resolved"

I am building an application that has a database of students, courses, and it also keeps track of all the courses that each student is taking,
I have an entity for Course and Student, and they have a one to many relationship with entity StudentCourses and an embeddable class of StudentCoursesID.
In the StudentCourse when I try to use the annotation AssociationOverride it gives me a problem saying
"Persistent type of override attribute "student" cannot be resolved"
"Persistent type of override attribute "course" cannot be resolved"
and
"Embedded ID class should not contain relationship mappings"
I dont understand if i did the mapping wrong or if there is a disconnection between my entities and classes.
Below I have each of the the entity starting with StudentCourses which is giving me issues.
StudentCourses
package jpa.entitymodels;
import javax.persistence.*;
#Entity
#Table(name = "student_courses")
#AssociationOverrides({
#AssociationOverride(name = "student", joinColumns = #JoinColumn(name = "sEmail")),
#AssociationOverride(name = "course", joinColumns = #JoinColumn(name = "cId"))
})
public class StudentCourses {
private StudentCoursesId id = new StudentCoursesId();
public StudentCourses() {
}
public StudentCourses(StudentCoursesId id) {
this.id = id;
}
#EmbeddedId
public StudentCoursesId getId() {
return id;
}
StudentCoursesId
package jpa.entitymodels;
import javax.persistence.Embeddable;
import javax.persistence.ManyToOne;
import java.io.Serializable;
#Embeddable
public class StudentCoursesId implements Serializable {
private static final long serialVersionUID = 1L;
private Student student;
private Course course;
public StudentCoursesId() {
}
#ManyToOne
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
#ManyToOne
public Course getCourse() {
return course;
}
Course
package jpa.entitymodels;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
#Entity
#Table(name = "course")
public class Course {
#Id
#Column(name = "id")
int cId;
#Column(name = "name")
String cName;
#Column(name = "instructor")
String cInstructorName;
#OneToMany(mappedBy = "id.course", fetch = FetchType.LAZY)
List<StudentCourses> studentCourses = new ArrayList<>();
Student
package jpa.entitymodels;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.*;
#Entity
#Table(name="student")
public class Student {
#Id
#Column(name = "email")
String sEmail;
#Column(name = "name")
String sName;
#Column(name = "password")
String sPass;
#OneToMany(mappedBy = "id.student", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
List<StudentCourses> studentCourses = new ArrayList<>();
The 'student' doesn't exist in StudentCourses which is why you get the error. Overrides you want to apply to the embeddable need to be defined on the embeddable mapping, not the StudentCourses class.
#Entity
#Table(name = "student_courses")
public class StudentCourses {
#Embedded
#AttributesOverride({
#AttributeOverride(name = "student", joinColumns = #JoinColumn(name = "sEmail")),
#AttributeOverride(nname = "course", joinColumns = #JoinColumn(name = "cId"))})
private EmbeddableClass embeddableClass;
public StudentCourses() {
}
}
JPA requires an ID class that has the simple pk types within it (or other nested composite PK classes, not relationships), requiring StudentCoursesId have a int/string property for the cId and sEmail. If you want to have mappings to course/student, maybe see the mapsId annotation or alternatively, I would map it like this:
#Entity
#Table(name = "student_courses")
#IdClass(StudentCoursesId.class)
public class StudentCourses {
#Id
#JoinColumn(name = "sEmail")
Student student;
#Id
#JoinColumn(name = "cId")
Course course;
}
public class StudentCoursesId implements Serializable {
private static final long serialVersionUID = 1L;
private String student;//Name matches id property in StudentCourses
private int course;
}

JPA Query mapping

I want to use orderNumber in ProductRepository but I keep getting SQL error. I mapped both sides.
this is product entity.
#Entity
public class Product extends BaseEntity {
#Id
#GeneratedValue
#Column(name = "product_id")
private Long id;
#ManyToOne
#JoinColumn(name = "orderItem_id")
private OrderItem orderItem;
this is orderItem entity.
#Entity
public class OrderItem extends BaseEntity {
#Id
#GeneratedValue
#Column(name = "orderItem_id")
private Long id;
private String orderNumber
#OneToMany(mappedBy = "orderItem", cascade = CascadeType.ALL, orphanRemoval = true)
#Builder.Default
private List<Product> products = new ArrayList<Product>();
this is query in ProductRepository.
#Query(value = "SELECT * FROM OrderItem a, Product b WHERE a.orderItem_id = b.orderItem_id", nativeQuery = true)
Product findByOrderNumber(String orderNumber);
Alternatively, you can try query by a method as shown below, you don't need to use the #Query in this case.
ProductRepository
Product findByOrderItemOrderNumber(String orderNumber);
you can find more details here

Spring Data -- Query JOIN Validation ERROR -- Validation failed for query for method

I'm trying to do a 3 table Join Query with Spring data and I'm running into a query validation issue.
The exact error I get is:
Caused by: java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List com.somethinng.domain.subscriberCategoriesRepository.findByJoin()!
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:92)
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.<init>(SimpleJpaQuery.java:62)
at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromMethodWithQueryString(JpaQueryFactory.java:72)
at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromQueryAnnotation(JpaQueryFactory.java:53)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$DeclaredQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:144)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:212)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:77)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:436)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:221)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:277)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:263)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:101)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
... 39 more
On the Database side here are my three tables
create table subscribers(
id int auto_increment primary key,
email varchar(255),
unique (email));
create table categories (
id int auto_increment primary key,
source varchar(255) not null,
description varchar(255) not null);
create table subscriberCategories(
subscriber int not null,
source int not null,
primary key (subscriber,source), -- prevents dupes
constraint `fk_2user` foreign key (subscriber) references subscribers(id));
And here are my POJO's and Repositories
Subscribers
#Entity
#Getter
#Setter
public class Subscribers {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
#NotNull
private String email;
public Subscribers() { }
public Subscribers(Integer id) {
this.id = id;
}
public Subscribers(String email, String description) {
this.email = email;
}
}
Categories
#Entity
#Getter
#Setter
public class Categories {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
#NotNull
private String description;
#NotNull
private String source;
public Categories() { }
public Categories(Integer id) {
this.id = id;
}
public Categories(String source, String description) {
this.source = source;
this.description = description;
}
}
SubscriberCategories
#Entity
#Getter
#Setter
public class subscriberCategories {
#Id
#Column(name = "subscriber")
private Integer id;
#NotNull
private Integer source;
public subscriberCategories() {
}
public subscriberCategories(Integer subscriberId) {
this.id = subscriberId;
}
public subscriberCategories(Integer source, Integer subscriberId) {
this.source = source;
this.id = subscriberId;
}
}
Repositories
SubscriberRepository
#Repository
public interface SubscriberRepository extends CrudRepository<Subscribers, Integer> {
}
CategoriesRepository
#Repository
public interface CategoriesRepository extends CrudRepository<Categories, Integer> {
List<Categories> findById(Integer id);
Long deleteBySource(String source);
List<Categories> findBySource(String source);
}
subscriberCategoriesRepository
#Transactional
#Repository
public interface subscriberCategoriesRepository extends CrudRepository<subscriberCategories, Integer> {
#Query(value = "SELECT DISTINCT s.email as Subscriber, c.source as Source from Subscribers s" +
"Inner Join subscriberCategories sc on s.id = sc.subscriber" +
"Inner Join Categories c on sc.subscriber = s.id where s.email = 'xxxxx#gmail.com'")
List<subscriberCategories> findByJoin();
Lastly I have the following unit test which is throwing the error when I run it
#RunWith(SpringRunner.class)
#SpringBootTest
#EnableJpaRepositories(basePackageClasses = subscriberCategories.class)
#Transactional
public class subscriberCategoriesTest {
#Autowired
subscriberCategoriesRepository subscriberCategoriesRepository;
#Test
public void testLoadCategories() {
List<subscriberCategories> subscriberCategories = (List<subscriberCategories>) subscriberCategoriesRepository.findByJoin();
assertEquals("Should contain something", 1, subscriberCategories.size());
}
If someone give me a hand with this I would really appreciate it
Thanks

Hibernate grouping and result set transformation

Suppose I have the following result set:
So, we have one-to-many-to-many relation here.
The question is what would be the best way to group it in Hibernate and convert this data structure to DTO with fields like:
String countryName
String companyName
List invoiceNumbers
Thank you!
For Country and Company ---- Many-to-Many
For Company and Invoice --- One-to-Many
#Entity
public class Country {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
#ManyToMany(mappedBy="countries")
private Collection<Country> countries;
...
getters and setters
}
#Entity
public class Company {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
#ManyToMany
private Collection<Country> countries;
#OneToMany
private Collection<Invoice> invoices;
...
getters and setters
}
#Entity
public class Invoice {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private int invoice_number;
...
getters and setters
}