JsonB, JPA, REST, CDI, Generating incomplete JSON - java

i'm trying to get Jsonb objects from my DB. When try getting all Lessons from the DB im getting an error
Schwerwiegend: Generating incomplete JSON
Warnung: StandardWrapperValve[rest.ApplicationConfig]: Servlet.service() for servlet rest.ApplicationConfig threw exception
java.lang.StackOverflowError
I figuered out that its because of the way my Class is build. My Lesson class has a List of questions showns below
#Entity
public class Lesson extends EntityWithIntId {
private String lessonName;
#OneToMany(mappedBy="lessonID",cascade = CascadeType.ALL)
private List<Question> questions = new ArrayList<>();
public Lesson() {
}
public Lesson(String lessonName) {
this.lessonName = lessonName;
}
public void addQuestion(Question question) {
this.questions.add(question);
}
// getter setter
}
Question Class:
#Entity
public class Question extends EntityWithIntId {
#ManyToOne
#JoinColumn(name = "lessonID")
private Lesson lessonID;
private String question;
#ElementCollection
#MapKeyColumn(name = "answer")
#Column(name = "solution")
#CollectionTable(name = "answers", joinColumns = #JoinColumn(name = "questionID"))
Map<String, Integer> answers = new LinkedHashMap<>();
public Question() {
}
public Question(Lesson lessonID) {
this.lessonID = lessonID;
}
public Question(String question) {
this.question = question;
}
public Question(Lesson lessonID, String question) {
this.lessonID = lessonID;
this.question = question;
}
public void addAnswer(String answer) {
this.answers.put(answer, 0);
}
// getter setter
}
So i tested a bit and noticed that if i build the connection bewteen Lesson and Question like this:
#Entity
public class Lesson extends EntityWithIntId {
private String lessonName;
#OneToMany(cascade = CascadeType.ALL)
#JoinColumn(name = "lessonID")
private List<Question> questions = new ArrayList<>();
}
#Entity
public class Question extends EntityWithIntId {
private int lessonID;
private String question;
#ElementCollection
#MapKeyColumn(name = "answer")
#Column(name = "solution")
#CollectionTable(name = "answers", joinColumns = #JoinColumn(name = "questionID"))
Map<String, Integer> answers = new LinkedHashMap<>();
}
Everything works as expected and i get my Json Object. The issue is that i cant change my whole DB structure and my programm only for this. I also have to use JsonB so there is no other choice.
There has to be a way to get a proper Json object from my current DB.
Here is the #Get method:
#GET
#Produces({MediaType.APPLICATION_JSON})
public Response getAllLesssons() {
if (super.findAll() != null) {
return Response.ok(super.findAll()).build();
} else {
String message = "Lessons not found!";
return Response.ok().type(MediaType.TEXT_PLAIN).entity(message).build();
}
}
Anyone knows how to solve this problem?

So i solved this issue by removing methods in my Entities which start with get.... and are not returning a Class variable. For examle getLessonById() or something like that. I guess JSON sees them as class variable returner and wants to get data from it. Also Bidirectional relations are causing endless loops so either you use specific annotations to ignore those or you just avoid them completly.

You can use annotation #JsonbTransient (javax.json.bind.annotation.JsonbTransient) to prevent serialization filds causes circle in the object graph.

Related

Save successful but Update failed - Many to Many Spring JPA with extra column using Jointable

Am getting an error while updating the existing entity with below approach using PostRepository save method.
Here are my objects,
#Entity
public class Post {
#Id
private String postId;
private String postName;
#OneToMany(mappedBy = "Post", cascade = CascadeType.ALL)
private Collection<PostTag> postTags = new HashSet<PostTag>();
}
#Entity
public class Tag {
#Id
private String tagId;
private String tagName;
#OneToMany(mappedBy = "tag", cascade = CascadeType.ALL)
#JsonIgnore
private Collection<PostTag> postTags = new HashSet<PostTag>();
}
#Entity
public class PostTag {
#EmbeddedId
private PostTagId postTagId = new PostTagId();
#ManyToOne
#MapsId("postId")
#JoinColumn(name = "post_Id")
#JsonIgnore
private Post post;
#ManyToOne
#MapsId("tagId")
#JoinColumn(name = "tag_Id")
private Tag tag;
//extra columns ommited
}
#Embeddable
public class PostTagId implements Serializable {
private String postId;
private String tagId;
//equals & hashcode ommited
}
I try to save the post as in the form of below POST json,
{
"postId": "post-001",
"postName": "post-001",
"postTags": [
{
"tag": {
"tagId": "tag-001",
"tagName": "tag-001"
}
}
]
}
Service implementation looks as below,
public Post save(Post post){
Post newPost = new Post();
newPost.setPostName(Post.getPostName());
newPost.setPostId(Post.getPostId());
for (PostTag posttag : post.getPostTags()) {
PostTag newPostTag = new PostTag();
Tag dbTag = tagRepo.getById(posttag.getTag().getTagId());
if(dbTag == null){
Tag newtag = new Tag();
newtag.setTagId(posttag.getTag().getTagId());
newtag.setTagName(posttag.getTag().getTagName());
tagRepo.save(newTag);
dbTag = newTag;
}
newPostTag.setTag(dbTag);
newPostTag.setPost(newPost);
newPost.getPostTags().add(newPostTag);
}
return PostRepository.save(newPost);
}
The above code works first time and creates record in POST & TAG & POSTTAG.
But when i run the save again with same input it complains below error,
javax.persistence.EntityExistsException: A different object with the same identifier value was already associated with the session : [PostTagId#c03f34a0]
Clearly it says there is already an obj which is there in PostId + TagId combination,
But how can i just do update or merge in that case for only PostTag entity extra fields if there is already same combination available?
Please help.
There was already same question on next link, so it maybe helps you: answer

More than one row with the given identifier was found when calling findAll(), but works if I iterate trough all entries with findById();

Well... I have an auto-generated database by hibernate and when I try to call findAll() in controller I receive that
More than one row with the given identifier was found: 1, for class: com.example.rentacar.domain.Masina
Anyway, I checked database I don t have duplicates keys. This is the controller:
#Controller
public class MasinaController {
MasinaService masinaService;
#Autowired
public MasinaController (MasinaService masinaService){
this.masinaService = masinaService;
}
#RequestMapping("/masini")
public String getMasini(Model model){
var masini = masinaService.findMasini();
model.addAttribute("masini", masini);
return "masini";
}
}
service:
#Autowired
public MasinaServiceImpl(MasinaRepo masinaRepo) {
this.masinaRepo = masinaRepo;
}
public List<Masina> findMasini(){
var masini = masinaRepo.findAll();
return masini;
}
this is the domain:
#Entity
#Data
public class Masina {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String model;
private Integer capacitateCilindrica;
private Integer putere;
private Integer anProductie;
private String culoare;
private String numarInmatriculare;
private Float pret;
private Boolean esteInchiriata;
#OneToOne(mappedBy = "masina")
private ChirieActiva ChirieActiva;
#OneToMany(mappedBy = "masina")
private List<ChirieFinalizata> ChiriiFinalizate;
#OneToOne
private Firma Firma;
#ManyToMany(mappedBy = "Masini",
cascade = {CascadeType.PERSIST, CascadeType.MERGE})
private List<Categorie> CategoriiMasina;
}
The rest of object that are linked are empty(no-entries).
I tried to see what identifier is corrupted so I iterate through all entries to see what id will return me an error. After full iteration none of the entries returned error..
The funniest part is that if I add this code in controller (iterate through all entries from db using findById before calling findAll) it WORKS !!!!
So.. my controller looks like this...
#RequestMapping("/masini")
public String getMasini(Model model){
for(int i=1; i<9;i++){
var masina = masinaService.findById(i);
}
var masini = masinaService.findMasini();
model.addAttribute("masini", masini);
return "masini";
}
Did anyone know why that happens? Thanks a lot!
I solved the problem by adding (fetch = FetchType.LAZY) to #OneToOne relationships.

Am I using OneToOne relationship right?

Hello I am actually working on a REST server using Spring-boot, hibernate, psql and I am experiencing some difficulties after adding an OneToOne relationship between entities.
Here are the 2 entities:
Pays:
#Entity
#Table(name = "pays")
public class Pays implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#JsonProperty("codePays")
private String codePays;
#Column(name = "libelle_pays")
#JsonProperty("libellePays")
private String libellePays;
#OneToOne(mappedBy = "pays",cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional=false)
private Traduction traduction;
protected Pays() {
}
public Pays(String codePays,String libellePays) {
this.codePays = codePays;
this.libellePays = libellePays;
}
and Traduction:
#Entity
#Table(name = "traduction")
public class Traduction implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#JsonProperty("codeTrad")
private long codeTrad;
#Column(name = "defaultLanguage")
#JsonProperty("defaultLanguage")
private boolean defaultLanguage;
#OneToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
#JoinColumn(name="fk_code_pays")
#JsonProperty("codePays")
private Pays pays;
public Traduction(){
}
public Traduction(String codePays,boolean defaultLanguage) {
this.defaultLanguage = defaultLanguage;
pays.setCodePays(codePays);
}
My problem happen when I try to populate my table traduction using a Post method:
#PostMapping("/traduction")
public Traduction createTraduction(#RequestBody Traduction trad) {
System.err.println(trad);
return repository.save(trad);
}
when I send JSON data to my server via PostMan like this:
{
"codeTrad":0,
"defaultLanguage":true,
"fk_code_pays":"FR"
}
or this way:
{
"codeTrad":0,
"defaultLanguage":true,
"pays":
{
"codePays":"FR",
"libellePays":"France"
}
}
My server doesn't seem to understand the mapping with the object Pays.
Here what the object Traduction look like after my request:
[codeTrad=0, null, defaultLanguage=true]
and the pretty error:
Resolved [org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: (was java.lang.NullPointerException); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: com.auchan.corp.ipon.iponportail.model.Traduction["codePays"])]
So I am wondering if the problem comes from my server conception or just my JSON. Do you have an idea?
Your issue comes from :
public Traduction(String codePays, boolean defaultLanguage) {
this.defaultLanguage = defaultLanguage;
pays.setCodePays(codePays)
The pays is null and that's why you get an exception : java.lang.NullPointerException, well try to add Pays pays to that constructor.
This json won't work:
{ "codeTrad":0, "defaultLanguage":true, "fk_code_pays":"FR" }
as there is no field name fk_code_pays in your Traduction class.
Below won't work either:
{ "codeTrad":0, "defaultLanguage":true, "pays": { "codePays":"FR", "libellePays":"France" } }
Because pays is annotated with #JsonProperty("codePays")
As per your DTO classes, your json should be:
{ "codeTrad":0, "defaultLanguage":true, "codePays": { "codePays":"FR", "libellePays":"France" } }
Also I would recommend you to use wrapper classes in place of primitives. Boolean in place of boolean and Long in place of long.

Failed to lazily initialize a collection of role during conversion of object to json

I am new to spring and while fetching records from a table having relationship with other tables getting this lazily initialling error.
I have read a lot online but not getting a appropriate approach.
Table1:
#SuppressWarnings("serial")
#Entity
public class Terminal extends BaseEntity {
#Column(length = 100, unique = true)
private String shortName;
#Column
private short number; // short stores up to 32767 value
#Column
private String description;
#OneToMany
#JoinColumn(name = "terminal_id", referencedColumnName = "uuid")
#Cascade({ CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DELETE })
private Set<BusinessHour> businessHour;
public String getShortName() {
return shortName;
}
public void setShortName(String shortName) {
this.shortName = shortName;
}
public short getNumber() {
return number;
}
public void setNumber(short number) {
this.number = number;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Set<BusinessHour> getBusinessHour() {
return businessHour;
}
public void setBusinessHour(Set<BusinessHour> businessHour) {
this.businessHour = businessHour;
}
Table2:
#SuppressWarnings("serial")
#Entity
public class BusinessHour extends BaseEntity {
#Column
private DayOfWeek dayOfWeek;
#Column
private LocalTime startOfOperation;
#Column
private LocalTime endOfOperation;
public DayOfWeek getDayOfWeek() {
return dayOfWeek;
}
}
Service Code:
#Service
public class TerminalServiceImpl implements TerminalService {
#Autowired
TerminalRepository terminalRepository;
Iterable<Terminal> allTerminals = terminalRepository.findAll();
List<Terminal> terminalList = new ArrayList<Terminal>();
for (Terminal terminal : allTerminals) {
terminalList.add(terminal);
}
return terminalList;
}
Terminal Repository code:
#Transactional
public interface TerminalRepository extends CrudRepository<Terminal, Long> {
}
Code where i got error during debug:
private List<Terminal> updateTerminalList() {
List<Terminal> allTerminals = terminalService.fetchAllTerminal();
return allTerminals;
}
public void terminalWrapperRun() {
try {
Payload payload = createTerminalPayload(applicationId);
String json3 = object2Json(payload);
kafkaRESTUtils.sendServerPayload(json3);
} catch (Exception e1) {
e1.printStackTrace();
}
}
public String object2Json(Object dataArray) throws JsonProcessingException {
return mapper.writeValueAsString(dataArray);
}
Error:
com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: terminal.model.Terminal.businessHour, could not initialize proxy - no Session (through reference chain:
Getting exception while converting fetching object to json. which i found due to proxy object return due to fetch type lazy(which i want to kept as it is).
I believe this issue relates to the LAZY loading of Collections by default by your ORM.
#OneToMany
#JoinColumn(name = "terminal_id", referencedColumnName = "uuid")
#Cascade({ CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DELETE })
private Set<BusinessHour> businessHour;
The #OneToMany annotation has a fetch property which is set to LAZY by default.
FetchType fetch() default LAZY;
OneToMany reference
This means that it will only be loaded when the data is accessed. In the case of your example, this will happen when you try to create the JSON string. By this point, however, you are outside the scope of the ORM session so it does not know how to load the data.
Therefore you have 2 options.
Change your annotation to eagerly load the data (which means the BusinessHour Set will be loaded at the same time as the parent object
#OneToMany(fetch = FetchType.EAGER)
perform your JSON generation within an ORM session (I would only really recommend doing this is the first option causes performance issues).
If I recall correctly this is the kind of error caused by an Entity being detached from the EntityManager at the time of its use (being it a Proxy it cannot perform a database query to retrive the data).
You can use:
#OneToMany(fetch = FetchType.EAGER)
...
private Set<BusinessHour> businessHour;
Using FetchType=EAGER means that any query against your entity will load the whole bunch of annotated entities.
Imho, this is only a sensible action if you are 100% sure that your entity will only be used for your special business case.
In all other cases - like programming a data acess as a library, or accepting different kinds of queries on your entities, you should use entity graphs (https://docs.oracle.com/javaee/7/tutorial/persistence-entitygraphs002.htm) or explicit loading (Hibernate.initialize, join-fetch, see for example https://vladmihalcea.com/hibernate-facts-the-importance-of-fetch-strategy/).
If your use case only is the transformation, you have two good options:
Transform your entity to JSON within a Transactional method (as PillHead suggested)
Load your entity explicitly with all the entities needed (via entity graphs or Hibernate.initialize) within the transaction, and then convert to JSON where you need it.

Spring-MVC, Hibernate : Creating DTO objects from Domain objects

I am working on a Spring-MVC application in which I am trying to search for List of GroupNotes in database. The mapping in my project is GroupCanvas has one-to-many mapping with GroupSection and GroupSection has one-to-many mapping with GroupNotes. Because of these mappings, I was getting LazyInitializationException. As suggested on SO, I should be converting the objects to a DTO objects for transfer. I checked on net, but couldnt find a suitable way to translate those.
I have just created a new List to avoid the error, but one field is still giving me an error. I would appreciate if anyone tells me either how to fix this error or convert the objects to a DTO objects so they can be transferred.
Controller code :
#RequestMapping(value = "/findgroupnotes/{days}/{canvasid}")
public #ResponseBody List<GroupNotes> findGroupNotesByDays(#PathVariable("days")int days, #PathVariable("canvasid")int canvasid){
List<GroupNotes> groupNotesList = this.groupNotesService.findGroupNotesByDays(days,canvasid);
List<GroupNotes> toSendList = new ArrayList<>();
for(GroupNotes groupNotes : groupNotesList){
GroupNotes toSendNotes = new GroupNotes();
toSendNotes.setMnotecolor(groupNotes.getMnotecolor());
toSendNotes.setNoteCreationTime(groupNotes.getNoteCreationTime());
toSendNotes.setMnotetag(groupNotes.getMnotetag());
toSendNotes.setMnotetext(groupNotes.getMnotetext());
toSendNotes.setAttachCount(groupNotes.getAttachCount());
toSendNotes.setNoteDate(groupNotes.getNoteDate());
toSendList.add(toSendNotes);
}
return toSendList;
}
GroupNotesDAOImpl :
#Override
public List<GroupNotes> searchNotesByDays(int days, int mcanvasid) {
Session session = this.sessionFactory.getCurrentSession();
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_YEAR, -days);
long daysAgo = cal.getTimeInMillis();
Timestamp nowMinusDaysAsTimestamp = new Timestamp(daysAgo);
GroupCanvas groupCanvas = (GroupCanvas) session.get(GroupCanvas.class,mcanvasid);
Query query = session.createQuery("from GroupSection as n where n.currentcanvas.mcanvasid=:mcanvasid");
query.setParameter("mcanvasid", mcanvasid);
List<GroupSection> sectionList = query.list();
List<GroupNotes> notesList = new ArrayList<GroupNotes>();
for (GroupSection e : sectionList) {
System.out.println("Section name is "+e.getMsectionname());
GroupSection groupSection = (GroupSection) session.get(GroupSection.class,e.getMsectionid());
Query query1 = session.createQuery("from GroupNotes as gn where gn.ownednotes.msectionid=:msectionid and gn.noteCreationTime >:limit");
query1.setParameter("limit", nowMinusDaysAsTimestamp);
query1.setParameter("msectionid",e.getMsectionid());
notesList.addAll(query1.list());
}
// I am getting the data below, but I get JSON errors.
for(GroupNotes groupNotes : notesList){
System.out.println("Group notes found are "+groupNotes.getMnotetext());
}
return notesList;
}
GroupCanvas model :
#Entity
#Table(name = "membercanvas")
public class GroupCanvas{
#OneToMany(mappedBy = "currentcanvas",fetch=FetchType.LAZY, cascade = CascadeType.REMOVE)
#JsonIgnore
private Set<GroupSection> ownedsection = new HashSet<>();
#JsonIgnore
public Set<GroupSection> getOwnedsection() {
return this.ownedsection;
}
public void setOwnedsection(Set<GroupSection> ownedsection) {
this.ownedsection = ownedsection;
}
}
GroupSection model :
#Entity
#Table(name = "membersection")
public class GroupSection{
#OneToMany(mappedBy = "ownednotes", fetch = FetchType.EAGER,cascade = CascadeType.REMOVE)
#JsonIgnore
private Set<GroupNotes> sectionsnotes = new HashSet<>();
public Set<GroupNotes> getSectionsnotes(){
return this.sectionsnotes;
}
public void setSectionsnotes(Set<GroupNotes> sectionsnotes){
this.sectionsnotes=sectionsnotes;
}
}
GroupNotes model :
#Entity
#Table(name="groupnotes")
public class GroupNotes{
#Id
#Column(name="mnoteid")
#GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "mnote_gen")
#SequenceGenerator(name = "mnote_gen",sequenceName = "mnote_seq")
#org.hibernate.annotations.Index(name = "mnoticesidindex")
private int mnoticesid;
#Column(name = "mnotetext")
private String mnotetext;
#Column(name = "mnoteheadline")
private String mnotetag;
#Column(name = "mnotecolor")
private String mnotecolor;
#Column(name = "mnoteorder")
private double mnoteorder;
#Column(name = "attachmentcount")
private int attachCount;
#Column(name = "notedate")
private String noteDate;
#Column(name = "uploader")
private String uploader;
#Column(name = "activeedit")
private boolean activeEdit;
#Column(name = "notedisabled")
private boolean noteDisabled;
#Column(name = "noteinactive")
private boolean noteInActive;
#Column(name = "notecreatoremail")
private String noteCreatorEmail;
#Column(name = "prefix")
private String prefix;
#Column(name = "timestamp")
private Timestamp noteCreationTime;
#Transient
private boolean notRead;
#Transient
private String tempNote;
#Transient
private String canvasUrl;
#ManyToOne
#JoinColumn(name = "msectionid")
#JsonIgnore
private GroupSection ownednotes;
#JsonIgnore
public GroupSection getOwnednotes(){return this.ownednotes;}
public void setOwnednotes(GroupSection ownednotes){this.ownednotes=ownednotes;}
#JsonIgnore
public int getOwnedSectionId(){
return this.ownednotes.getMsectionid();
}
#OneToMany(mappedBy = "mnotedata",fetch = FetchType.LAZY,cascade = CascadeType.REMOVE)
#JsonIgnore
private Set<GroupAttachments> mattachments = new HashSet<>();
public Set<GroupAttachments> getMattachments() {
return this.mattachments;
}
public void setMattachments(Set<GroupAttachments> mattachments) {
this.mattachments = mattachments;
}
#OneToMany(mappedBy = "mhistory",fetch = FetchType.LAZY,cascade = CascadeType.REMOVE)
#JsonIgnore
private Set<GroupNoteHistory> groupNoteHistorySet = new HashSet<>();
public Set<GroupNoteHistory> getGroupNoteHistorySet(){
return this.groupNoteHistorySet;
}
public void setGroupNoteHistorySet(Set<GroupNoteHistory> groupNoteHistorySet){
this.groupNoteHistorySet = groupNoteHistorySet;
}
#OneToMany(mappedBy = "unreadNotes",fetch = FetchType.LAZY,cascade = CascadeType.REMOVE)
#JsonIgnore
private Set<UnreadNotes> unreadNotesSet = new HashSet<>();
public Set<UnreadNotes> getUnreadNotesSet(){
return this.unreadNotesSet;
}
public void setUnreadNotesSet(Set<UnreadNotes> unreadNotesSet){
this.unreadNotesSet = unreadNotesSet;
}
//getters and setters ignored
}
Error log :
org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: (was java.lang.NullPointerException) (through reference chain: java.util.ArrayList[0]->com.journaldev.spring.model.GroupNotes["ownedSectionId"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: java.util.ArrayList[0]->com.journaldev.spring.model.GroupNotes["ownedSectionId"])
Kindly let me know what to do, as I am stuck on that error since some time.
What I think that happens is Jackson tries to serialize all fields in the hierarchy based on getter methods. In some situation NullPointerException is thrown in the following method:
#JsonIgnore
public int getOwnedSectionId(){
return this.ownednotes.getMsectionid();
}
replace it with the following method:
#JsonIgnore
public int getOwnedSectionId(){
if(this.ownednotes != null)
return this.ownednotes.getMsectionid();
return 1;
}
I don't have an explanation why jackson tries to serialize it when is market with #JsonIgnore but you can give a try with my proposal
I would appreciate if anyone tells me either how to fix this error or convert the objects to a DTO objects so they can be transferred.
We use DozerMapper at work for this purpose.
Instead of doing that mapping manually you might want to take a look at Blaze-Persistence Entity Views which can be used to efficiently implement the DTO pattern with JPA. Here a quick code sample how your problem could be solved
First you define your DTO as entity view
#EntityView(GroupNotes.class)
public interface GroupNoteView {
#IdMapping("mnoticesid") int getId();
String getMnotecolor();
String getMnotetag();
String getMnotetext();
String getNoteDate();
Timestamp getNoteCreationTime();
int getAttachCount();
}
Next you adapt your DAO to make use of it
#Override
public List<GroupNoteView> searchNotesByDays(int days, int mcanvasid) {
EntityManager entityManager = // get the entity manager from somewhere
CriteriaBuilderFactory cbf = // factory for query building from Blaze-Persistence
EntityViewManager evm = // factory for applying entity views on query builders
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_YEAR, -days);
long daysAgo = cal.getTimeInMillis();
Timestamp nowMinusDaysAsTimestamp = new Timestamp(daysAgo);
CriteriaBuilder<GroupNotes> cb = cbf.create(entityManager, GroupNotes.class, "note")
.where("noteCreationTime").gt(nowMinusDaysAsTimestamp)
.where("ownednotes.ownedcanvas.mcanvasid").eq(mcanvasid);
return evm.applySetting(EntityViewSetting.create(GroupNoteView.class), cb)
.getResultList();
}
And finally the calling code
#RequestMapping(value = "/findgroupnotes/{days}/{canvasid}")
public #ResponseBody List<GroupNoteView> findGroupNotesByDays(#PathVariable("days")int days, #PathVariable("canvasid")int canvasid){
return this.groupNotesService.findGroupNotesByDays(days, canvasid);
}

Categories