Joining two tables without JPQL - java

I have to MySQL tables TICKET-is the parent and USER-is the child, in a many-to-one relationship.
=TICKET=
PK(ID)
summary
message
FK(user_id) references USER(user_id)
=USER=
PK(ID)
email
password
And the JPA entities
#Entity
class TICKET {
#Id
private Integer ID;
private String summary;
private String message;
#ManyToOne
#JoinColumn(name="user")
private USER user;
}
#Entity
class USER {
#Id
private Integer ID;
private String email;
private String password;
}
If i make a query to get a ticket by ID it will return also the user information (USER.ID, USER.email, USER.password) which is not good.
ticketsCrudRepository.findById(ticketId);
What i want is to get a table that looks like this:
TICKET.ID | summary | message | USER.email
I know how to do it in MySQL but JPA it's to much for me. I don't want to use JPQL or native query language.
Any suggestions?

When you fetch a Ticket you also get a User since #ManyToOne by default has FetchType.EAGER. You can change this by changing the anotation to #ManyToOne(fetch = FetchType.LAZY)
To do the search you chould try somethig like
public List<Object[]> getTikcetsForUser(final User user) {
String hql = "select t.id, t.summary, t.message, u.email "
+ "from Tikcet t, User u "
+ "where ticket.user = :user";
Query<Object[]> query = getSession().createQuery(hql, Object[].class);
query.setParameter("user", user);
return query.getResultList();
}
The returned List will contain arrays with 4 fields (t.id, t.summary, t.message, u.email).
Please let me know if you find this useful.
:)

The solution i was looking for was Spring Data JPA Projections. They can be interfaces or classes with getters that match the columns you want to get from the database. For detailed informations check the Spring Framework documentation here.

Using Spring Data Projections is one option, but you will at some point run into the limitations it has. If you reach that point, you can look into Blaze-Persistence Entity Views.
I created the library to allow easy mapping between JPA models and custom interface or abstract class defined models, something like Spring Data Projections on steroids. The idea is that you define your target structure(domain model) the way you like and map attributes(getters) via JPQL expressions to the entity model.
A DTO model for your use case could look like the following with Blaze-Persistence Entity-Views:
#EntityView(TICKET.class)
public interface TicketDto {
#IdMapping
Long getId();
String getName();
UserDto getUser();
#EntityView(USER.class)
interface UserDto {
#IdMapping
Long getId();
String getEmail();
}
}
Or even simpler in your case
#EntityView(TICKET.class)
public interface TicketDto {
#IdMapping
Long getId();
String getName();
#Mapping("user.email")
String getUserEmail();
}
Querying is a matter of applying the entity view to a query, the simplest being just a query by id.
TicketDto a = entityViewManager.find(entityManager, TicketDto.class, id);
The Spring Data integration allows you to use it almost like Spring Data Projections: https://persistence.blazebit.com/documentation/entity-view/manual/en_US/index.html#spring-data-features

Related

jpa entitymanager - map query result into nested dto classes

I'm using jpa EntityManager with hibernate in my java spring application. Suppose I have a user entity like below:
public class User {
private Long id;
...
#ManyToOne
private Address address
}
And I have Custom user dto object for passing into client:
public class UserDTO {
private Long id;
private AddressDTO address;
...
}
And I have a UserRepository that exceute normal jpql query with EntityManager and Query.
Note, I need to have custom dto, because my dto has some fields that does not exist in entity and must be calculated in query. Now my question: is there any way with EntityManager that map flat query result into my nested UserDTO? In fact, I need to map result of address in AdressDTO inside UserDto and so on.
Note: I want to use jpql not native sql query.
You can construct DTO right in JPQL.
Here is an example.
select new your.package.UserDTO(u.id, a.country, a.city, a.street)
from User u join u.address a
where ...
Such query returns List<UserDTO>.
Of course UserDTO has to have appropriate constructor:
public UserDTO(Long id, String country, String city, String street){
this.id = id;
this.address = new AddressDTO(country, city, street);
}
You're on the right way.
You really need to fetch User and then convert it to UserDTO.
Don't build DTO within your queries.
For that conversion you need Java Mapper. I prefer MapStruct but there is a plenty of such tools (ModelMapper, Dozer etc).
MapStruct is smart enough to manage nested objects as well.

How do I populate two fields of entity using Hibernate?

I am building an API to return two fields as such:
{
currentPoints: 325,
badgeName: "Some Badge"
}
However, I am having trouble using hibernate in order populate those two fields. I made two attempts and both are throwing errors. Both of these errors can be found in their respective Repository file. In the 2nd attempt, I am using native=true and am able to get it to work using a SELECT *. However, I am trying to only populate and return two fields of the entity.
One solution I thought about is using the 2nd approach with a SELECT * and creating another package named response with CurrentInfoResponse class and just returning that class. However, I wanted to see if there was a way to avoid this using the current model that I have.
Possible Solution:
#Getter
#AllArgsConstructor
public class CurrentInfoResponse{
private Integer currentPoints;
private String badgeName
}
Package Structure:
Controller.java:
#GetMapping("/current-badge/{userId}")
public CurrentBadgeInfoModel getCurrentBadge(#PathVariable Integer userId){
return currentBadgeInfoService.getCurrentBadge(userId);
}
ServiceImpl.java:
#Override
public CurrentBadgeInfoModel getCurrentBadge(Integer userId){
return currentBadgeInfoRepository.getCurrentBadge(userId);
}
CurrentBadgeInfoModel.java:
#Getter
#Entity
#Table(name = "user_current_badge_info")
public class CurrentBadgeInfoModel {
#Id
#Column(name = "user_current_info_id")
private Integer userCurrentBadgeInfo;
#Column(name = "user_id")
private Integer userId;
#Column(name = "current_points")
private Integer currentPoints;
#ManyToOne
#JoinColumn(name = "badge_id")
private BadgeModel badgeModel;
}
BadgeModel.java
#Getter
#Entity
#Table(name = "badge_info")
public class BadgeModel {
#Id
#JoinColumn(name= "badge_id")
private Integer badgeId;
#Column(name = "badge_name")
private String badgeName;
}
Repository.java - ATTEMPT 1:
#Repository
public interface CurrentBadgeInfoRepository extends JpaRepository<CurrentBadgeInfoModel, Integer> {
#Query("SELECT cbim.currentPoints, cbim.badgeModel.badgeName FROM CurrentBadgeInfoModel cbim JOIN
cbim.badgeModel WHERE cbim.userId=?1")
CurrentBadgeInfoModel getCurrentBadge(Integer userId);
}
//Error: No converter found capable of converting from type [java.lang.Integer] to type [com.timelogger.model.CurrentBadgeInfoModel]
Repository.java - ATTEMPT 2:
#Repository
public interface CurrentBadgeInfoRepository extends JpaRepository<CurrentBadgeInfoModel, Integer> {
#Query(value = "SELECT current_points, badge_name FROM user_current_badge_info ucbi JOIN badge_info bi ON ucbi.badge_id=bi.badge_id WHERE user_id=?1", nativeQuery = true)
CurrentBadgeInfoModel getCurrentBadge(Integer userId);
}
//Error: Column 'user_current_info_id' not found
Using the SELECT clause of HQL should help you here.
If you don't have that constructor, you can add it
#Query("SELECT new CurrentBadgeInfoModel(cbim.currentPoints, cbim.badgeModel.badgeName) FROM CurrentBadgeInfoModel cbim JOIN
cbim.badgeModel WHERE cbim.userId=?1")
Notice the usage of new CurrentBadgeInfoModel(cbim.currentPoints, cbim.badgeModel.badgeName)
I think this is a perfect use case for Blaze-Persistence Entity Views.
I created the library to allow easy mapping between JPA models and custom interface or abstract class defined models, something like Spring Data Projections on steroids. The idea is that you define your target structure(domain model) the way you like and map attributes(getters) via JPQL expressions to the entity model.
A DTO model for your use case could look like the following with Blaze-Persistence Entity-Views:
#EntityView(CurrentBadgeInfoModel.class)
public interface CurrentInfoResponse {
Integer getCurrentPoints();
#Mapping("badgeModel.badgeName")
String getBadgeName();
}
The Spring Data integration allows you to use it almost like Spring Data Projections: https://persistence.blazebit.com/documentation/entity-view/manual/en_US/index.html#spring-data-features
CurrentInfoResponse findByUserId(Integer userId);
The best part is, it will only fetch the state that is actually necessary!

Select only some columns from a table

Is there a way to select only some columns from a table using jpa?
My tables are huge and I am not allowed to map all the columns in my entities. I tried to create an entity (as a side note, I don't have PKs in my tables):
#Entity
#Table(name = "SuperCat")
#Getter
#Setter
public class Cat{
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
#Column(name="nameCat")
private String name;
}
and then in my repository to
public interface CatRepository extends
CrudRepository<Cat, Long> {
#Query(
"SELECT name FROM Cat")
Page<Cat> getAlCats(Pageable pageable);
This is only a simple example, but the idea is the same. I have searched a lot and I found projections, but there you need to map the whole table, then I found native queries, but still doesn't apply. I know I can return an Object and the other solution is to use query with NEW and create my own object (no #entity, like a pojo). But is there a way that I can do this using jpa, to be able to use repository and services, if I am creating my own pojo then i will create a #transactional class put the queries (with NEW) there and this is it. I don't like this approach and I don't think that the jpa does't allow you to select only some columns, but I didn't find a proper way.
Maybe you will ask what is the result if I am doing like this:
I get this error: "Cannot create TypedQuery for query with more than one return using requested result type [java.lang.Long]"
(For new queries, I am talking about : http://www.java2s.com/Tutorials/Java/JPA/4800__JPA_Query_new_Object.htm maybe I was not clear)
You can do the same by using below approach.
Just create a constructor in entity class with all the required parameters and then in jpa query use new operator in query like below.
String query = "SELECT NEW com.dt.es.CustomObject(p.uniquePID) FROM PatientRegistration AS p";
TypedQuery<CustomObject> typedQuery = entityManager().createQuery(query , CustomObject.class);
List<CustomObject> results = typedQuery.getResultList();
return results;
And CustomObject class should look like below with the constructor.
public class CustomObject {
private String uniquePID;
public CustomObject(String uniquePID) {
super();
this.uniquePID = uniquePID;
}
public String getUniquePID() {
return uniquePID;
}
public void setUniquePID(String uniquePID) {
this.uniquePID = uniquePID;
}
}
spring-data-jpa projection not need to map the whole table, just select the necessary fileds :
// define the dto interface
public interface CatDto {
String getName();
// other necessary fields
...
}
#Query(value = "select c.name as name, ... from Cat as c ...)
Page<CatDto> getAllCats(Pageable pageable);
By this way, CatDto is an interface and it only includes some fileds part of the whole table. Its fields name need to match the select field's alias name.

what to use instead of fetch.EAGER in spring data?

I'm working with spring-boot and angular5 , i have this entity in spring :
#Entity
#Data
#AllArgsConstructor
#NoArgsConstructor
public class Contrat implements Serializable{
#Id #GeneratedValue
private Long id;
private Date dateDebut ;
private Date dateFin ;
#ManyToOne
#JoinColumn(name = "Id_Project")
#JsonBackReference(value="projet-contrat")
private Project project;
#ManyToOne
#JoinColumn(name = "Id_AppUser")
#JsonBackReference(value="appuser-contrat")
private AppUser appUser;
}
A repository :
public interface ContratRepo extends JpaRepository<Contrat,Long> {
public Page<Contrat> findByAppUser(#Param("userApp") AppUser userApp, Pageable pageable);
}
As the fetch.lazy is the default one , when i try to call the method findByAppUser i get as result :
{id: 1, dateDebut: 1526083200000, dateFin: 1526083200000}
Which is normal , what i want for my case is to load also the object 'project' that exists in the entity , but i don't wan't to use the fetch.EAGER , any solution for this goal ?
Your entity is one-many relationship object. If you don't use EAGER, spring data will get the object without related member object. And if you get that with contract.getProject().getName(), then another query will be sent to get that member.
If you log the SQL, you can see that, there will be 2 queries. But if you set the field as EAGER, there will be only 1 query. You can get improvement obviously.
But you should not use EAGER always. If in 90% of time, you just need the Contract object, but no need the project data of it. It is a waste of time to get that. Because in SQL, it will relate 2 tables and get all columns of data.
SO, you should make this decision based on your usage of this entity.
[Updated based on comment]
You can use Query to write your sql expression. for example, I have a method to get the entity with detail:
#Query("select s from Contract s left join fetch s.project pr where s.id = ?1 ")
Contract findOneWithDetail(Long id);
If I need to get the detail in ONE sql, I can use this method. If I don't need the project detail, I just use findOne(Long id), which is provided interface.
And, if you just want to get some columns, you need to define a DTO, with a constructor, and write your method like this:
#Query("SELECT NEW com.mypackage.dto.ContractDTO(s.id, s.name, s.status) FROM Contract AS s WHERE s.status = ?1")
List<ContractDTO> findDTOAllByStatus(String status);
Provide the query in your repo method, e.g. (syntax may be wrong, just show you the idea)
public interface ContratRepo extends JpaRepository<Contrat,Long> {
#Query(query="from Contrat c left join fetch c.project " +
"where c.userApp = :userApp")
public Page<Contrat> findByAppUser(#Param("userApp") AppUser userApp, Pageable pageable);
}

Hibernate table mappings with annotations

I'm just getting started with Spring/Hibernate and I'm trying to understand how hibernate maps entities to tables. Right now I'm working with the following classes to test out CRUD operation with hibernate:
#Entity
#Table(name = "Users")
public class UserEntity implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String firstName;
private String lastName;
private int age;
//Getters, setters, etc.
}
#Repository
public class UserDAOImpl implements UserDAO {
#Autowired
HibernateTemplate hibernateTemplate;
#Override
public UserEntity getUser(String firstName, String lastName) {
List<UserEntity> users = hibernateTemplate.find("from UserEntity u where u.firstName = ? and u.lastName = ?", firstName, lastName);
return (users.size() == 0 ? null : users.get(0));
}
#Override
public void saveOrUpdate(UserEntity user) {
hibernateTemplate.saveOrUpdate(user);
}
}
The code above is working fine, but I'm confused how UserEntity is being mapped to a table. More specifically, when calling hibernateTemplate.find("from UserEntity u where u.firstName = ? and u.lastName = ?", firstName, lastName) I get the following message in the log:
Hibernate: select userentity0_.id as id0_, userentity0_.age as age0_, userentity0_.firstName as firstName0_, userentity0_.lastName as lastName0_ from Users userentity0_ where userentity0_.firstName=? and userentity0_.lastName=?
When calling hibernateTemplate.find() I'm required to specify the table UserEntity whereas the log reports it's querying the table Users (as I specified with the #Table annotation).
Ideally I'd like to be consistent throughout my program and refer to the UserEntity table as Users rather than UserEntity. Is there some way I can do this? If so can I do it with HQL or would I need to write native SQL queries instead?
When you write HQL you are querying against the OO model. So in this case, your class name UserEntity is used. However, hibernate will translate this into a native SQL query and thus in the logs you will see references to users table. I'm not sure what your question is though. If the classname is UserEntity then throughout your application you should use UserEntity. I think that going into native SQL is only necessary when the HQL does not do what you want.
Vincent explained the matter, I'll just add some some clarifications.
The #Table annotation is optional, and merely indicates what the underlying table is (by default it is the same as the entity name (passing through a translation mechanism).
But the idea of hibernate (and orm layers) is to let the developer write code without any reference to the underlying relational model whatsoever. And that's what you should try to do - "forget" about the relational model beneath and use only the object model.

Categories