Spring Boot API returns json without labels - java

I'm building a rest API with Java Spring Boot and I'm running into a problem, I have the following class with a method (which is in my controller for testing purposes, I will send its logic to the service later):
#RestController
#RequestMapping("/api/readings")
public class Readings {
#Autowired
private ReadingService readingService;
#RequestMapping(method = RequestMethod.GET, produces = "application/json")
public List<Reading> getRelevant(#RequestParam("start") String start, #RequestParam("end") String end){
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:MM:SS");
start += " 00:00:00";
end += " 23:59:59";
try {
Date startdate = df.parse(start);
Date enddate = df.parse(end);
return readingService.getRelevant(startdate, enddate);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
}
This makes use of a service that calls the following repository function:
#Query("SELECT pmtwofive, pmten, recording FROM Reading WHERE recording >= ?1 AND recording <= ?2")
List<Reading> getRelevant(Date start, Date end);
Everything works fine, except for the format of the result:
[[10,20,1505801743816],[14,21,1505802311976],[14,21,1505802330610],[10,13,1505803302960],[10,13,1505803321966]]
Instead of this, I was expecting something like I get when using the CrudRepository from hibernate querying my whole table instead of just these three values:
{
{
pmtwofive: 10,
pmten: 20,
reading: 1505801743816
},
{
...
}
}
What should I do to get my expected result? Thank you!
Reading Class:
package com.amione.models;
import javax.persistence.*;
import java.sql.Timestamp;
#Entity
public class Reading {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(columnDefinition = "serial")
private long reading_id;
private int sensor_id;
private int function;
private int directionstart;
private int pmtwofive;
private int pmten;
private int checksumlow;
private int checksumhigh;
private Timestamp recording;
public long getReading_id() {
return reading_id;
}
public void setReading_id(int reading_id) {
this.reading_id = reading_id;
}
public int getSensor_id() {
return sensor_id;
}
public void setSensor_id(int sensor_id) {
this.sensor_id = sensor_id;
}
public int getFunction() {
return function;
}
public void setFunction(int function) {
this.function = function;
}
public int getDirectionstart() {
return directionstart;
}
public void setDirectionstart(int directionstart) {
this.directionstart = directionstart;
}
public int getPmtwofive() {
return pmtwofive;
}
public void setPmtwofive(int pmtwofive) {
this.pmtwofive = pmtwofive;
}
public int getPmten() {
return pmten;
}
public void setPmten(int pmten) {
this.pmten = pmten;
}
public int getChecksumlow() {
return checksumlow;
}
public void setChecksumlow(int checksumlow) {
this.checksumlow = checksumlow;
}
public int getChecksumhigh() {
return checksumhigh;
}
public void setChecksumhigh(int checksumhigh) {
this.checksumhigh = checksumhigh;
}
public Timestamp getRecording() {
return recording;
}
public void setRecording(Timestamp recording) {
this.recording = recording;
}
}

Ok I have the answer. It must be done with custom constructor:
#Data
#EqualsAndHashCode(callSuper = true)
#Entity
public class City extends AbstractEntity {
#Column
private String name;
#Embedded
private Geopoint centre=new Geopoint();
public City(){}
public City(String name){
this.setName(name);
}
// #OneToMany(mappedBy="city")
// private Set<Place> places;
}
Repository:
public interface CityRepository extends JpaRepository<City, Long>{
City findOneByName(String name);
#Query("SELECT name FROM City")
public List<City> findMethod1();
#Query("SELECT c.name FROM City c")
public List<City> findMethod2();
Controller:
#Autowired
private CityRepository cityRepository;
#GetMapping("/test")
public List<City> test(){
List<City> ret=new ArrayList();
ret.addAll(cityRepository.findMethod1());
ret.addAll(cityRepository.findMethod2());
ret.addAll(cityRepository.findMethod3());
return ret;
}
and the result:
As you can see, 3rd method works. I told you it will came up.
As empty values are still serialized, you can use a DTO object to encapsule only required fields (and SELECT new EntityDTO(field1,field2,field3) FROM Entity)
Another option would bo to configure Jackson to not to serialize null values with annotation or configuratio, but that is just beyond the scope of question.

Related

could not serialize; nested exception is org.hibernate.type.SerializationException: could not serialize

I have spend way too much find finding the root cause of the below error:
org.springframework.orm.jpa.JpaSystemException: could not serialize; nested exception is org.hibernate.type.SerializationException: could not serialize
I am trying to save some value to db:
public void logFailure(Long objectID,Integer usLK){
StatusFailureDO failureDO = new StatusFailureDO(4,objectID, usLK);
failuresRepository.save(failureDO.getFailure());
}
#Repository
public interface FailuresRepository extends JpaRepository<GeneralFailure, Integer> {
GeneralFailure save(GeneralFailure aGeneralFailure);
void delete(GeneralFailure aGeneralFailure);
GeneralFailure findByObjectID(Long objectID);
}
There were many mapping errors and as such that I got pass now. I am trying to understand where in the process error occurs and what shall I look out for.
public class StatusFailureDO extends GeneralFailureDO implements Serializable
{
public StatusFailureDO(Integer failureTypeLK,Long objectID,
Integer usLK)
{
super(new StatusFailure(failureTypeLK,
"An exception occurred while trying to update an UploadStatus entry.",
objectID, usLK));
}
//more constructors and setters
}
public abstract class GeneralFailureDO implements ICISConstant, Serializable
{
private GeneralFailure mGeneralFailure;
//constructors and setters
}
#Entity
#Inheritance(strategy = InheritanceType.JOINED)
#Table(name = "GEN_FLR")
public class GeneralFailure implements Serializable,ICISConstant
{
#Column(name = "CRTN_TM")
private Date mCreationTime;
#Column(name = "TYP_LKP_ID")
private Integer failureTypeLK;
#Column(name = "STUS_LKP_ID")
private Integer mFailureStatusLK;
#Column(name="OBJ_ID")
#Id
#GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
#GenericGenerator(name = "native", strategy = "native")
private Long objectID;
#Column(name = "DSCR")
private String mDescription;
public Date getCreationTime()
{
return mCreationTime;
}
public void setCreationTime(Date aCreationTime)
{
mCreationTime = aCreationTime;
}
public String getDescription()
{
return mDescription;
}
public void setDescription(String aDescription)
{
if (aDescription != null && aDescription.length() > MAX_DESCRIPTION_LENGTH)
{
mDescription = aDescription.substring(0, MAX_DESCRIPTION_LENGTH);
}
else
{
mDescription = aDescription;
}
}
public Long getObjectID()
{
return objectID;
}
public void setObjectID(Long aObjectID)
{
objectID = aObjectID;
}
public Integer getFailureTypeLK()
{
return failureTypeLK;
}
public void setFailureTypeLK(Integer aFailureTypeLK)
{
failureTypeLK = aFailureTypeLK;
}
public Integer getFailureStatusLK()
{
return mFailureStatusLK;
}
public void setFailureStatusLK(Integer aFailureStatusLK)
{
mFailureStatusLK = aFailureStatusLK;
}
}
#Entity
#Table(name="STUS_FLR")
public class StatusFailure extends GeneralFailure implements Serializable
{
#Column(name = "STUS_OBJ_ID")
private Long mStatusObjectID;
#Column(name = "STUS_LKP_ID")
private Integer mStatusLK;
#Column(name = "RQST_TYP_LKP_ID")
private Integer mRequestTypeLK;
#Column(name = "CODE")
private String mCode;
#Column(name = "PST_TM")
private Timestamp mPostTime;
#Column(name = "MSG_SZ")
private Integer mMessageSize;
#OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Collection<StatusFailureError> StatusFailureErrorList;
#Column(name = "SMPL_FLG")
private boolean mSimple;
public Integer getStatusLK()
{
return mStatusLK;
}
public void setStatusLK(Integer statusLK)
{
mStatusLK = statusLK;
}
public Long getStatusObjectID()
{
return mStatusObjectID;
}
public void setStatusObjectID(Long statusObjectID)
{
mStatusObjectID = statusObjectID;
}
public String getCode()
{
return mCode;
}
public void setCode(String aCode)
{
mCode = aCode;
}
public Collection<StatusFailureError> getStatusFailureErrorList()
{
return mStatusFailureErrorList;
}
public void setStatusFailureErrorList(
Collection<StatusFailureError> aStatusFailureErrorList)
{
mStatusFailureErrorList = aStatusFailureErrorList;
}
public void setErrorList(Collection<String> aErrorList)
{
if (aErrorList != null && !aErrorList.isEmpty())
{
mStatusFailureErrorList = new ArrayList<StatusFailureError>();
for (Iterator<String> iter = aErrorList.iterator(); iter.hasNext();)
{
String error = (String) iter.next();
StatusFailureError failureError = new StatusFailureError(this, error, getPostTime());
mStatusFailureErrorList.add(failureError);
}
}
else
{
mStatusFailureErrorList = null;
}
}
public Integer getMessageSize()
{
return mMessageSize;
}
public void setMessageSize(Integer aMessageSize)
{
mMessageSize = aMessageSize;
}
public Timestamp getPostTime()
{
return mPostTime;
}
public void setPostTime(Timestamp aPostTime)
{
mPostTime = aPostTime;
}
public Integer getRequestTypeLK()
{
return mRequestTypeLK;
}
public void setRequestTypeLK(Integer aRequestTypeLK)
{
mRequestTypeLK = aRequestTypeLK;
}
public boolean isSimple()
{
return mSimple;
}
public void setSimple(boolean aSimple)
{
mSimple = aSimple;
}
}
Any help is really appreciated.
It is not obvious what the failureDO.getFailure() returns exactly because you did not provide a method definition for the StatusFailureDO.getFailure() method and so I will assume that that method returns an instance of a GeneralFailure class (or StatusFailure that extends it).
For hibernate to successfully save objects into the database, the #Entity classes that you are trying to save need to consist of "base" types. I see that you have an object of class CLRISCache defined in your GeneralFailure data class, that is most definitely not of a base type and not another #Entity. You can prevent a field from being persisted by marking it with the #Transient annotation, but really you should keep your data class pure.
You can find a list of "base" types here: https://docs.jboss.org/hibernate/orm/5.0/mappingGuide/en-US/html/ch03.html
Actually I found the reason. The General failure has dateCreation variable of Date type and in mu status failure I had it as a timestamp. I need to make it to Date and it worked.

Can not store list of objects as single column JSON

I'm trying to store a whole array of object into one field on my oracle database, I'm referring to the solution on this question, but it kept giving me Can not set java.lang.String field xxx.demo.Models.Sensors.amplitudos to xxx.demo.Models.Sensors error, I have checked the JSON body and the entity class, but I cannot find the mistake.
Here is my code.
entity
#Entity
#Table(name = "SENSOR")
public class Sensor implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private long id;
#Column(name = "TIMERECEIVED")
private Timestamp timereceived;
#Column(name = "SENSORS")
private Sensors[] sensors;
#Column(name = "LOC")
private String location;
public Sensor() {
}
public Sensor(Timestamp timereceived, Sensors[] sensors, String location) {
this.timereceived = timereceived;
this.sensors = sensors;
this.location = location;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Timestamp getTimereceived() {
return timereceived;
}
public void setTimereceived(Timestamp timereceived) {
this.timereceived = timereceived;
}
public Sensors[] getSensors() {
return sensors;
}
public void setSensors(Sensors[] sensors) {
this.sensors = sensors;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
Sensors class
#Embeddable
public class Sensors {
private String amplitudos;
private Double displacement;
private String frequencies;
private Integer sensorId;
public Sensors() {
}
public Sensors(String amplitudos, Double displacement, String frequencies, Integer sensorId) {
this.amplitudos = amplitudos;
this.displacement = displacement;
this.frequencies = frequencies;
this.sensorId = sensorId;
}
public String getAmplitudos() {
return amplitudos;
}
public void setAmplitudos(String amplitudos) {
this.amplitudos = amplitudos;
}
public Double getDisplacement() {
return displacement;
}
public void setDisplacement(Double displacement) {
this.displacement = displacement;
}
public String getFrequencies() {
return frequencies;
}
public void setFrequencies(String frequencies) {
this.frequencies = frequencies;
}
public Integer getSensorId() {
return sensorId;
}
public void setSensorId(Integer sensorId) {
this.sensorId = sensorId;
}
}
my JSON body
{
"timereceived": "2022-11-29T12:04:42.166",
"sensors": [
{
"amplitudos": "a1#a2#a3#a4",
"displacement": 0.002,
"frequencies": "f1#f2#f3#f4",
"sensorid": 1
},
{
"amplitudos": "a1#a2#a3#a4",
"displacement": 0.002,
"frequencies": "f1#f2#f3#f4",
"sensorid": 2
},
{
"amplitudos": "a1#a2#a3#a4",
"displacement": 0.002,
"frequencies": "f1#f2#f3#f4",
"sensorid": 3
},
{
"amplitudos": "a1#a2#a3#a4",
"displacement": 0.002,
"frequencies": "f1#f2#f3#f4",
"sensorid": 4
}
],
"location": "lokasi"
}
my controller
#PostMapping("/sendData")
public ResponseEntity sendData(#RequestBody Sensor sensor) {
Sensor newSensor = sensorRepository.save(sensor);
System.out.println(newSensor);
return ResponseEntity.ok("Sensor received");
}
I have tried checking every possible solution and the problem is not fixed, my expectation is the data stored into 1 column for the sensors field in the JSON body.
The problem is with the JPA mapping, not with the Controller, I think.
You're using #Embeddable, which normally result in a set of columns in your main table. If it's a collection of #Embeddable objects, you could map it to a separate table with foreign keys, using #ElementCollection.
However, you want to store the collection of sensors as a single JSON string in a single column in your main table. For that, you do not need the #Embeddable annotation. You need to write a custom convertor to convert the collection of sensors to JSON.
public class SensorsConverter implements AttributeConverter<List<Sensors>, String> {
private final ObjectMapper objectMapper = new ObjectMapper();
#Override
public String convertToDatabaseColumn(List<Sensors> sensors) {
return objectMapper.writeValueAsString(sensors);
}
#Override
public List<Sensors> convertToEntityAttribute(String sensorsJSON) {
return objectMapper.readValue(sensorsJSON, new TypeReference<List<Sensors>>() {});
}
}
Then you can use it in your entity class:
#Column(name = "SENSORS")
#Convert(converter = SensorsConverter.class)
private List<Sensors> sensors;

Springboot posgresql reposity bean can't be autowired

I have simple Sprinngboot app where actual database is PostgreSQL.
My model:-
#Table("carrier")
#Entity
public class MyCarrier {
#Id
#Column("id")
private UUID id;
#Size(
max = 100
)
#Column("carrier_name")
private String carrierName;
#Size(
max = 3
)
#Column("smdg_code")
private String smdgCode;
#Size(
max = 4
)
#Column("nmfta_code")
private String nmftaCode;
public MyCarrier() {
}
public UUID getId() {
return this.id;
}
public String getCarrierName() {
return this.carrierName;
}
public String getSmdgCode() {
return this.smdgCode;
}
public String getNmftaCode() {
return this.nmftaCode;
}
public void setId(final UUID id) {
this.id = id;
}
public void setCarrierName(final String carrierName) {
this.carrierName = carrierName;
}
public void setSmdgCode(final String smdgCode) {
this.smdgCode = smdgCode;
}
public void setNmftaCode(final String nmftaCode) {
this.nmftaCode = nmftaCode;
}
protected boolean canEqual(final Object other) {
return other instanceof Carrier;
}
}
Repository:-
#Repository
public interface MyCarrierRepository extends JpaRepository<MyCarrier, Long> {
}
Controller:-
#RestController
#RequestMapping(path = "/upload")
public class ReactiveUploadResource {
Logger LOGGER = LoggerFactory.getLogger(ReactiveUploadResource.class);
private final SqlRequestHandler sqlRequestHandler;
#Autowired
private MyCarrierRepository myCarrierRepository;
public ReactiveUploadResource(SqlRequestHandler sqlRequestHandler) {
this.sqlRequestHandler = sqlRequestHandler;
}
}
I got this error:-
Description:
Field myCarrierRepository in com.consumer.controller.ReactiveUploadResource required a bean of type 'com.consumer.repository.MyCarrierRepository' that could not be found.
What is missing? Why Springboot doesn't find this repository?
You have to put the repository inside the package at the same level as Application class the packages to allow Spring boot to scan it

Room #Insert does not insert all records

I am running into an issue where only 1 record is being inserted into my Room SQLite DB.
When I perform a getAll(); the result only returns 1 record.
FOUND ISSUE: Genre[] genres = gson.fromJson(jsonArray.toString(), Genre[].class);
This line above is setting all "gid" values to 0, and I am not sure how to change that.
Genre.java
#Entity(indices = {#Index(value = {"id", "name"}, unique = true)})
public class Genre {
#PrimaryKey
private int gid;
//#ColumnInfo(name = "id") By Default - No need to annotate
#NonNull
private int id;
private String name;
public int getGid() {
return gid;
}
public void setGid(int gid) {
this.gid = gid;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
GenreDao.java
#Dao
public interface GenreDao {
#Query("SELECT * FROM Genre")
LiveData<List<Genre>> getAll();
#Insert(onConflict = OnConflictStrategy.REPLACE) //If there is a conflict, replace the record.
void insertAll(Genre... genres);
}
GenreRepository.java
public class GenreRepository {
private final GenreDao genreDao;
public GenreRepository(GenreDao genreDao) {
this.genreDao = genreDao;
}
//Database Methods
public void insertAll(Genre... genres) {
AsyncTask.execute(() -> { //Same as new Runnable()
genreDao.insertAll(genres);
});
}
public LiveData<List<Genre>> getAll() {
return genreDao.getAll();
}
}
APIUtil.java - getGenres() Method
This class makes an API call, returns the proper results, converts the JSONArray to a Genre[]. I can successfully loop through the Genre[] and confirm 10+ results come back.
public static void getGenres(Context context) {
APIWrapper wrapper = new APIWrapper(context, API_KEY);
Parameters params = new Parameters();
params.addFields(GENRE_FIELDS);
params.addLimit("50");
wrapper.genres(params, new onSuccessCallback() {
#Override
public void onSuccess(JSONArray jsonArray) {
Gson gson = new Gson();
Genre[] genres = gson.fromJson(jsonArray.toString(), Genre[].class);
//Insert DB
AppDatabase db = AppDatabase.getAppDatabase(context);
GenreRepository genreRepository = new GenreRepository(db.genreDao());
genreRepository.insertAll(genres);
}
#Override
public void onError(VolleyError volleyError) {
Log.e("GENRES ERROR:", volleyError.toString());
}
});
}
GenreViewModel.java
public class GenreViewModel extends ViewModel {
private GenreRepository genreRepository;
public GenreViewModel(GenreRepository genreRepository) {
this.genreRepository = genreRepository;
}
public void insertAll(Genre... genres){
genreRepository.insertAll(genres);
}
public LiveData<List<Genre>> getAll(){
return genreRepository.getAll();
}
}
SearchFragment.java
This is where I am retrieving the database values. This for loop only returns 1 result.
AppDatabase db = AppDatabase.getAppDatabase(getActivity());
GenreRepository genreRepository = new GenreRepository(db.genreDao());
GenreViewModel genreViewModel = new GenreViewModel(genreRepository);
genreViewModel.getAll().observe(this, genres -> { //new Observer<List<Genre>>()
for(Genre g : genres){
Log.e("GENRE", g.getName());
}
});
public void insertAll(Genre... genres){
genreRepository.insertAll(genres);
}
here is your mistake , what you provide as method definition and what you provide at call. see you make some thing wrong.
Solution
void insertAll(List<T> obj);
you can try with convert your array to list and put above in definition
I had this problem too.
And Solved it this way.
The problem was that the id that comes from server was mongoId and String so I should create a int primary key and pass currentTime as value to it so the database can insert all of them not replace them.
But you should consider using System.nanoTime() method instead of System.currentTimeMillis() cause sometimes it generates same value and then room replace them instead of inserting each one of them.

Spring Data JPA - Get the values of a non-entity column of a custom native query

I am using Spring Boot/MVC.
I have a custom query using JpaRepository:
public interface WorkOrderRepository extends JpaRepository<WorkOrder, Integer> {
#Query(value = "SELECT * FROM (SELECT * FROM workorder) Sub1 INNER JOIN (SELECT wo_number, GROUP_CONCAT(service_type SEPARATOR ', ') AS 'service_types' FROM service_type GROUP BY wo_number) Sub2 ON Sub1.wo_number=Sub2.wo_number WHERE fleet_company_id=?1 AND (order_status='On-Bidding' OR order_status='Draft')", nativeQuery = true)
Collection<WorkOrder> findWorkOrdersByFleet(Long fleetCompanyID);
}
It returns the following table:
http://imgur.com/Ylkc6U0
As you can see it has service_types columns which is a result of Concat, it's not part of the entity class. My problem is how can I get the value of that column. Some said I can use a separate DTO to map the service_types column? Or I can use 'new' keyword? Maybe you have other worked on me. I also tried to make a transient column service_types but it didn't work.
This is my entity class:
#Entity
#Table(name="workorder")
public class WorkOrder {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name="wo_number")
private Long woNumber;
#ManyToOne(optional=false, cascade=CascadeType.ALL)
#JoinColumn(name = "vehicle_id")
private Vehicle vehicle;
#ManyToOne(optional=false, cascade=CascadeType.ALL)
#JoinColumn(name = "fleet_company_id")
private FleetCompany fleetCompany;
#Column(name="order_title")
private String orderTitle;
#Column(name="order_date")
private String orderDate;
#Column(name="order_time")
private String orderTime;
#Column(name="order_status")
private String orderStatus;
#Column(name="ref_number")
private String refNumber;
#Column(name="proposals")
private int proposals;
//#Column(name="serviceTypes")
#Transient
private int serviceTypes;
public WorkOrder() {
super();
}
public Long getWoNumber() {
return woNumber;
}
public void setWoNumber(Long woNumber) {
this.woNumber = woNumber;
}
public String getOrderTitle() {
return orderTitle;
}
public void setOrderTitle(String orderTitle) {
this.orderTitle = orderTitle;
}
public String getOrderDate() {
return orderDate;
}
public void setOrderDate(String orderDate) {
this.orderDate = orderDate;
}
public String getOrderTime() {
return orderTime;
}
public void setOrderTime(String orderTime) {
this.orderTime = orderTime;
}
public String getOrderStatus() {
return orderStatus;
}
public void setOrderStatus(String orderStatus) {
this.orderStatus = orderStatus;
}
public String getRefNumber() {
return refNumber;
}
public void setRefNumber(String refNumber) {
this.refNumber = refNumber;
}
public int getProposals() {
return proposals;
}
public void setProposals(int proposals) {
this.proposals = proposals;
}
public Vehicle getVehicle() {
return vehicle;
}
public void setVehicle(Vehicle vehicle) {
this.vehicle = vehicle;
}
public FleetCompany getFleetCompany() {
return fleetCompany;
}
public void setFleetCompany(FleetCompany fleetCompany) {
this.fleetCompany = fleetCompany;
}
public int getServiceTypes() {
return serviceTypes;
}
public void setServiceTypes(int serviceTypes) {
this.serviceTypes = serviceTypes;
}
}
Some people told me to make a DTO:
public class WorkOrderDTO extends WorkOrder {
private String service_types;
public WorkOrderDTO() {
super();
}
public WorkOrderDTO(String service_types) {
this.service_types = service_types;
}
public String getService_types() {
return service_types;
}
public void setService_types(String service_types) {
this.service_types = service_types;
}
}
and add make the repository replaced from WorkOrder to WorkOrderDTO.
public interface WorkOrderRepository extends JpaRepository<WorkOrderDTO, Integer>
but when I do that I have autowiring problems.
I solved my own problem, finally!!!
I used #SqlResultMapping
SqlResultSetMapping(
name="workorder",
classes={
#ConstructorResult(
targetClass=WorkOrderDTO.class,
columns={
#ColumnResult(name="wo_number", type = Long.class),
#ColumnResult(name="service_types", type = String.class),
#ColumnResult(name="order_title", type = String.class)
}
)
}
)
And I created a new POJO that is not an entity named WorkOrderDTO.
#PersistenceContext
private EntityManager em;
#Override
public Collection<WorkOrderDTO> getWork() {
Query query = em.createNativeQuery(
"SELECT Sub1.wo_number, Sub2.service_types, Sub1.order_title FROM (SELECT * FROM workorder) Sub1 INNER JOIN (SELECT wo_number, GROUP_CONCAT(service_type SEPARATOR ', ') AS 'service_types' FROM service_type GROUP BY wo_number) Sub2 ON Sub1.wo_number=Sub2.wo_number WHERE fleet_company_id=4 AND (order_status='On-Bidding' OR order_status='Draft')", "workorder");
#SuppressWarnings("unchecked")
Collection<WorkOrderDTO> dto = query.getResultList();
Iterable<WorkOrderDTO> itr = dto;
return (Collection<WorkOrderDTO>)itr;
}
At last, the users who hated me for posting the same problem won't be annoyed anymore.

Categories