QueryException: could not resolve property - java

Here is my two pojo fields
#ManyToMany
#Cascade(value = SAVE_UPDATE)
#JoinTable(name = "catalogToCatalog",
joinColumns = {#JoinColumn(name = "parentId",
foreignKey = #ForeignKey(ConstraintMode.NO_CONSTRAINT))},
inverseJoinColumns = {#JoinColumn(name = "childId",
foreignKey = #ForeignKey(ConstraintMode.NO_CONSTRAINT))})
private List<Catalog> parents;
#Cascade(value = SAVE_UPDATE)
#ManyToMany(mappedBy = "parents", fetch = FetchType.EAGER, targetEntity = Catalog.class)
private List<Catalog> children;
How I could get data with named query like
"from Catalog c left join c.parents p " +
"where p.parentId = :parentId";
?
QueryException: could not resolve property: parentId of: com.x.model.Catalog [from com.x.model.Catalog c left join

Related

how to count items of OneToMany element via Spring data

i have a user Entity :
#Entity
#Table(name = "user")
#Data
public class UserEntity extends BaseEntity {
#Column(name = "full_name")
private String fullName;
....
#OneToMany(cascade = CascadeType.REMOVE)
#JoinTable(name = "user_post",
joinColumns = {#JoinColumn(name = "user_id")},
inverseJoinColumns = {#JoinColumn(name = "post_id")})
private Set<PostEntity> posts;
#ManyToMany
#JoinTable(name = "user_followers",
joinColumns = {#JoinColumn(name = "user_id", referencedColumnName = "id")},
inverseJoinColumns = {#JoinColumn(name = "follower_id", referencedColumnName = "id")})
private Set<UserEntity> followers = new HashSet<>();
#ManyToMany
#JoinTable(name = "user_followings",
joinColumns = {#JoinColumn(name = "user_id", referencedColumnName = "id")},
inverseJoinColumns = {#JoinColumn(name = "following_id", referencedColumnName = "id")})
private Set<UserEntity> followings = new HashSet<>();
}
now i want to count followers of a user.
i can get list of followers and use list.size() but this is not good . i just want count of them not whole objects .is this possible by a method in my UserRepostiory that extends JpaRepostiory or a native query ?
I suggest you to use JPQL syntax. In your UserRepository you can add count methods. Please try :
#Query("select count(p) from User u join u.posts p where u.id = ?1")
long countPosts(Long id);
#Query("select count(f) from User u join u.followers f where u.id = ?1")
long countFollowers(Long id);
#Query("select count(f) from User u join u.followings f where u.id = ?1")
long countFollowings(Long id);

How to Ignore a whole Mapped/Joined Model(Class) in Spring hibernate.

I have three tables. tbl_vendor and tbl_category tables are mapped using #ManyToMany relation. When I am getting 'Vendors' under a particular 'Category' I am also getting 'Areas' because tbl_vendor and tbl_area are also mapped using #ManyToMany.
relations:
in tbl_vendor Model :
#ManyToMany(cascade = CascadeType.DETACH, fetch = FetchType.LAZY)
#JoinTable(name = "tbl_category_vendor", joinColumns = {#JoinColumn(name = "vendor_id")}, inverseJoinColumns = {#JoinColumn(name = "category_id")})
private Set<Category> categories = new HashSet<>();
....
#ManyToMany(cascade = CascadeType.DETACH, fetch = FetchType.LAZY)
#JoinTable(name = "tbl_area_vendor", joinColumns = {#JoinColumn(name = "vendor_id")}, inverseJoinColumns = {#JoinColumn(name = "area_id")})
private Set<Area> areas = new HashSet<>();
in tbl_category Model :
#JsonIgnore
#ManyToMany(cascade = CascadeType.DETACH, mappedBy = "categories", fetch = FetchType.LAZY)
private List<Vendor> vendors = new ArrayList<>();
in tbl_area Model :
#JsonIgnore
#ManyToMany(mappedBy = "areas", fetch = FetchType.LAZY)
private List<Vendor> vendors = new ArrayList<>();
I have a particular case where I don't need this 'Area' with 'Vendor'.
is there any way to ignore already mapped 'Area' from 'Vendor' using JpaRepository in some particular case ?

Hibernate multiple attribute mappings same table deletion

I have a hopefully simple problem in Hibernate. I have Users and Projects. The users_projects (in my case user_project_association) has an extra attribute "role" to say if the user is project leader or normal user.
#OneToMany(mappedBy = "project", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
#LazyCollection(LazyCollectionOption.EXTRA)
private Set<UserProjectAssociation> userProjectAssociations = new HashSet<>();
#ManyToMany(fetch = FetchType.EAGER)
#JoinTable(name = "user_project_role",
joinColumns = #JoinColumn(name = "project_id", referencedColumnName = "id"),
inverseJoinColumns = #JoinColumn(name = "user_id", referencedColumnName = "id"))
#WhereJoinTable(clause = "role_id=7")
#Setter(AccessLevel.NONE)
private Set<User> projectLeaders = new HashSet<>();
#ManyToMany
#JoinTable(name = "user_project_role",
joinColumns = #JoinColumn(name = "project_id", referencedColumnName = "id"),
inverseJoinColumns = #JoinColumn(name = "user_id", referencedColumnName = "id"))
#LazyCollection(LazyCollectionOption.EXTRA)
#Setter(AccessLevel.NONE)
private Set<User> users = new HashSet<>();
If i try to remove a project via "getEntityManager().remove(persistentInstance);" I can see in the log this sql statements:
Hibernate:
/* delete collection com.XXX.core.model.Project.users */ delete
from
user_project_role
where
project_id=?
Hibernate:
/* delete collection com.XXX.core.model.Project.projectLeaders */ delete
from
user_project_role
where
project_id=?
and (
role_id=7
)
Hibernate:
/* delete com.XXX.core.model.UserProjectAssociation */ delete
from
user_project_role
where
id=?
and the error message:
2016-05-25 10:05:57 INFO AbstractBatchImpl:208 - HHH000010: On release of batch it still contained JDBC statements
and nothing is deleted. Does anyone have an idea to get my projects deleted?
Thanks a lot :)
Puh. I found (the or a) solution for it. It's not really elegant, but it's working:
#ManyToMany(fetch = FetchType.EAGER)
#JoinTable(name = "user_project_role",
joinColumns = #JoinColumn(name = "project_id", referencedColumnName = "id"),
inverseJoinColumns = #JoinColumn(name = "user_id", referencedColumnName = "id"))
#WhereJoinTable(clause = "role_id=7")
#SQLInsert(sql = "insert into user_project_role (project_id, user_id, role_id) values (?, ?, 7)")
#SQLDelete(sql = "delete from user_project_role where project_id=? and user_id=? and role_id = 7")
#Setter(AccessLevel.NONE)
private Set<User> projectLeaders = new HashSet<>();
#ManyToMany
#JoinTable(name = "user_project_role",
joinColumns = #JoinColumn(name = "project_id", referencedColumnName = "id"),
inverseJoinColumns = #JoinColumn(name = "user_id", referencedColumnName = "id"))
#LazyCollection(LazyCollectionOption.EXTRA)
#WhereJoinTable(clause = "role_id=8")
#SQLInsert(sql = "insert into user_project_role (project_id, user_id, role_id) values (?, ?, 8)")
#Setter(AccessLevel.NONE)
private Set<User> projectUsers = new HashSet<>();
So I removed the userProjectAssociations attribute and added customized #SQLInsert and #SQLDelete Statements.
If I want to get all Users including normal users and project leaders I use this method:
public Set<User> getAllUsers() {
Set<User> allUsers = new HashSet<>();
allUsers.addAll(projectLeaders);
allUsers.addAll(projectUsers);
return allUsers;
}

Hibernate create wrong query on joining 'many to many' association

Here is my entity classes:
AccService.java
#ManyToMany(mappedBy = "accServices")
private List<BaseSpecification> baseSpecifications;
#ManyToMany(mappedBy = "accServices")
private List<IndustrySpecification> industrySpecifications;
BaseSpecification.java
#ManyToMany(cascade = CascadeType.ALL)
#JoinTable(name = "BASE_SPEC_SERVICES",
joinColumns = {#JoinColumn(name = "SPEC_ID", referencedColumnName = "ID")},
inverseJoinColumns = {#JoinColumn(name = "SRV_ID", referencedColumnName = "ID")})
private List<AccService> accServices;</code>
IndustrySpecification.java
#ManyToMany(cascade = CascadeType.ALL)
#JoinTable(name = "IND_SPEC_SERVICES",
joinColumns = {#JoinColumn(name = "SPEC_ID", referencedColumnName = "ID")},
inverseJoinColumns = {#JoinColumn(name = "SRV_ID", referencedColumnName = "ID")})
private List<AccService> accServices;</code>
And I'm making query using Criteria API:
Criteria criteria = session.createCriteria(BaseSpecification.class);
criteria.createAlias("accServices", "as");
criteria.createCriteria("as.industrySpecifications", "asis", JoinType.LEFT_OUTER_JOIN, Restrictions.eq("asis.id", industrySpecId));
Hibernate generates following query, which fails because of using wrong alias in first 'left outer join' clause
select this_.name as y0_, asis3_.name as y1_,
from BASE_SPEC this_
inner join BASE_SPEC_SERVICES accservice5_ on this_.id=accservice5_.SPEC_ID
inner join ACC_SERVICE as2_ on accservice5_.SRV_ID=as2_.id
left outer join IND_SPEC_SERVICES industrysp7_ on as2_.id=industrysp7_.SRV_ID and ( asis3_.id=? )
left outer join INDUSTRY_SPEC asis3_ on industrysp7_.SPEC_ID=asis3_.id and ( asis3_.id=? ) ;

JPA many to one/one to many query

I would like to create JPA query based on this tables
**category**
(pk)CategoryID int (10)
category VARCHAR (45)
**templatecat**
(pk/fk)templateId int(10)
(pk/fk)categoryId int (10)
**template**
(pk)templateId int (10)
template madiumtext
I also have a "tempaltecat" table that holds foreign keys for category and template table.I`d like to create query that finds all templates in template table based on category and vice versa.
Here is my table mapping
#Entity
#Table(name = "category")
#OneToMany(cascade = CascadeType.ALL)
#JoinTable(name = "templatecat", joinColumns = { #JoinColumn(name = "categoryId", unique = true) }, inverseJoinColumns = { #JoinColumn(name = "templateId") })
private Set<Template> template;
#Entity
#Table(name = "template")
#ManyToOne(optional = true)
#JoinTable(name = "templatecat", joinColumns = { #JoinColumn(name = "templateId") }, inverseJoinColumns = { #JoinColumn(name = "categoryId") })
private Category category;
Thanks in advance
It looks like a #ManyToMany relationship,
Instead of using #OneToMany and #ManyToMany, you can use the following configuration:
In Category class:
#ManyToMany(cascade = CascadeType.ALL)
#JoinTable(name = "templatecat", joinColumns = { #JoinColumn(name = "categoryId", unique = true) }, inverseJoinColumns = { #JoinColumn(name = "templateId") })
private Set<Template> templates;
In Template class:
#Entity
#Table(name = "template")
#ManyMany(optional = true, mappedBy="templates");
private Set<Category> categories;
If you want to see all Templates of a given Category, the query would be:
select o.templates from Category o where o.id = ?
The reverse works as well (all Categories from a Template)
select o.categories from Template o where o.id = ?
Hope it has helped you.

Categories

Resources