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

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;
}

Related

How to make JPA JOIN query list giving only one item instead of all items by condition

I have some Entities looks like this. Very abstract:
#Entity
class Man {
#Id
String name;
#OneToMany(fetch = FetchType.EAGER, mappedBy = "name", cascade = CascadeType.ALL)
List<Car> carList;
}
#IdClass(TypeId.class)
#Entity
class Car {
#Id
#NonNull
String name;
#Id
#NonNull
String class;
#ManyToOne(fetch = FetchType.EAGER, optional = false)
#JoinColumn(name = "class",
referencedColumnName = "class",
insertable = false,
updatable = false)
Engine engine;
}
#Entity
class Engine() {
#Id
#NonNull
String class;
String type;
Integer count;
}
class TypeId {
String name;
String class;
}
I need to construct #Query - select MAN with condition: if Engine types are equals, I need to take one Car with less Engine count. And return Man with only one Car if condition is met. Otherwise return Man with all cars.

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

Making SQL/JPQL query to select all topics that matches both keywords

Challenge:
I want to create a query that selects Topics that match both the keyword ID´s "Java" and "sql", the Topic with ID = 1 matches both keywords "Java" and "Sql.
I have implemented two simple entities Topic and Keyword. They have a bidirectional ManyToMany relationship:
#Entity
#Table(name = "Topic")
public class Topic implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "topicID")
private Long id;
#Column(name = "topicTitle")
private String title = "";
#ManyToMany
#JoinTable(name = "Join_Topic_Keyword",
joinColumns = #JoinColumn(name = "Topic_ID"),
inverseJoinColumns = #JoinColumn(name = "Keyword_ID"))
private Set<Keyword> keywords;
}
#Entity
#Table(name = "Keyword")
public class Keyword implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "keywordID")
private String id;
#ManyToMany(mappedBy = "keywords")
private Set<Topic> topics;
}
Here is the structure table.
And This is the result from Join_Topic_Keyword

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

#Query Spring Data JPA Update Not Working

Here is my User entity:
#Entity
#Table(name="users")
public class User implements IBaseEntity<User> {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private Long userId;
#Column(unique = true)
#NotNull
private String username;
#Column(unique = true)
#NotNull
private String email;
#Column
#NotNull
private String password;
// #formatter:off
#ManyToMany(cascade = CascadeType.ALL)
#JoinTable(name = "users_roles",
joinColumns = { #JoinColumn(name = "user_id") },
inverseJoinColumns = { #JoinColumn(name = "role_id") })
// #formatter:on
private List<Role> roles = new ArrayList<Role>();
#Column
#NotNull
private Boolean locked;
...
}
Here is my Role entity:
#Entity
#Table(name="roles")
public class Role implements IBaseEntity<Role> {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private Long roleId;
#Column(unique = true)
#NotNull
private String name;
...
}
Here is my User service:
package org.quickloanconnect.service;
public interface IUsersService extends IBaseService<User>{
public void updateUserRolesById(List<Role> roles, Long userId);
...
}
Here is the UserServiceImpl :
#Service
#Transactional
public class UsersServiceImpl extends AbstractServiceImpl<User> implements
IUsersService {
...
#Override
#Transactional
public void updateUserRolesById(List<Role> roles, Long user_id) {
userDao.updateUserRolesById(roles, user_id);
}
...
}
And here is the dao:
public interface IUsersDao {
...
#Modifying
#Query("UPDATE User u SET u.roles = :roles WHERE u.userId = :userId")
public void updateUserRolesById(#Param("roles") List<Role> roles,
#Param("userId") Long userId);
...
}
When I run this update, I get the following: SqlExceptionHelper : No value specified for parameter 2 . What is causing this? When I update my user with a single role, I see that a List of size one is getting to the dao with the correct userId (both parameters present), but the update seems to fail at the dao level with the "SqlExceptionHelper : No value specified for parameter 2" error message.