How to preserve the order of objects in a Java collection? - java

I have following method which works fine however returns the objects in the alphabetical orders whenever being called from calling method :-
public List<Object> getExportDataFromView() {
try {
String selectQuery = "SELECT UNIQUE_ID, DATE, CODE, PRODUCT_CODE, "
+ " MESSAGE AS MESSAGE_TEXT, SOURCE, COMPANY_ID, REMARK, BATCH, COUNTRY, PRODUCT_CODE_SCHEME, EXTERNAL_TRANSACTION_ID, RETURN_CODE, CORRELATION_ID, BUSINESS_PROCESS, BATCH_EXPIRY_DATE "
+ " FROM \"test\".\"my-systems.Company::views.Export_Data_View\" "
+ " WHERE COMPANY_ID = 'C100' "
+ " ORDER BY DATE DESC";
Query q = this.em.createNativeQuery(selectQuery, ExportData.class);
return q.getResultList();
} catch (Exception e) {
return null;
} finally {
this.em.close();
}
}
Whenever this method is called from another calling method, the List returned is in sorted order (alphabetically). However, in the JPA Entity class of ExportData.java, the fields are declared in original order like below :-
#Entity
#Table(name = "\"my-systems.Company::views.Export_Data_View\"", schema="\"test\"")
public class ExportData implements Serializable {
private static final long serialVersionUID = 1L;
#Id
private String UNIQUE_ID;
private String CODE;
private String PRODUCT_CODE;
private String MESSAGE_TEXT;
private String SOURCE;
Is there any way to preserve the same order as declared in Entity class rather than being sorted without changing the return type i.e. List.
Any help would be highly appreciated.
Thank you

Related

How can I fetch database rows in Pojo without using JOOQ code generation?

I'm trying to use JOOQ without code generation. I have a dao class that looks like this
public class FilesDao {
public List<FilePojo> getAllFiles() {
DataSource dataSource = DataSourceFactory.getTestiDataSource();
List<FilePojo> filePojos = new ArrayList<>();
try (Connection con = dataSource.getConnection()) {
DSLContext create = DSL.using(con, SQLDialect.MARIADB);
filePojos = create.select(field("tiedosto.id"), field("tiedosto.nimi"), field("tiedosto.koko_tavua"),
field("tiedosto.sisalto"), field("tiedosto.hlo_id"))
.from(table("tiedosto"))
.where(field("minioupload").eq((byte) 0))
.fetch().into(FilePojo.class);
} catch (SQLException e) {
e.printStackTrace();
}
return filePojos;
}
}
and a Pojo class that looks like this
import javax.persistence.Column;
import javax.persistence.Table;
#Table(name="tiedosto")
public class FilePojo {
#Column(name = "id")
private Integer id;
#Column(name = "hlo_id")
private Integer customerId;
#Column(name = "koko_tavua")
private Integer fileSize;
#Column(name = "nimi")
private String fileName;
#Column(name = "sisalto")
private byte[] content;}
//Getters setters omitted
When I try to read from the table using a main method like this
public class App {
public static void main(String[] args) {
FilesDao mydao = new FilesDao();
List<FilePojo> myList = mydao.getAllFiles();
for (FilePojo filePojo : myList) {
System.out.println("==========================================" + "\n" +
filePojo.getId() + " " +
filePojo.getCustomerId() + " " +
filePojo.getFileName() + " " +
filePojo.getFileSize() + " " +
filePojo.getContent() + " " +
"==========================================");
}
}
}
The output is as follows
I can see that the SQL query is running fine and listing all the matched rows, but pojo is returning null values. What am I doing wrong here? Can someone please point me to the right direction? I'd really appreciate any sort of help.
I'm undecided whether this is a bug or a feature. You're using the plain SQL templating API when you should probably be using the identifier building API. When you write
field("tiedosto.id")
Then, jOOQ (possibly erroneously) thinks that your column is named `tiedosto.id`, with a period in the name. When it should really be qualified as `tiedosto`.`id`. There are a few possible fixes:
Keep using the plain SQL templating API
But then, don't qualify the name:
field("id")
Use the identifier building API
field(name("tiedosto", "id"))
Use the code generator
This should always be your preferred option, of course.

Neo4j OGM find node with their related nodes

I have one to many relationship(Model 1->N Field), I want find all model with their fields. but the model's fields is empty object.
#NodeEntity
public class Model {
#Id
private String id;
private String name;
#Relationship(type = "ModelField", direction = Relationship.OUTGOING)
private List<Field> fields = new ArrayList<Field>();
}
#NodeEntity
public class Field {
#Id
private String id;
private String name;
}
public interface ModelRepo extends Neo4jRepository<Model, String>{
}
public ModelRepo mr;
Iterable<Model> models = mr.findALl();
// the model's fields is empty
I just reconstructed your scenario basing on your correct code and it works fine. I assume by populating your data something went wrong. Because that piece of code is missing, I can’t point you to the concrete reason. To help you nevertheless, I outline my steps to retrieve your field nodes.
Adding a FieldRepo repository:
public interface FieldRepo extends Neo4jRepository<Field, String> {
}
Populating your scenario:
Model model = new Model("modelId1", "Model1");
Field field = new Field("fieldId1", "Field1");
model.getFields().add(field);
Field field2 = new Field("fieldId2", "Field2");
model.getFields().add(field2);
modelRepo.save(model);
fieldRepo.save(field);
fieldRepo.save(field2);
Retrieving your information:
Iterable<Model> resultModels = modelRepo.findAll();
for (Model resultModel : resultModels) {
System.out.println("Model: " + resultModel.getId() + " " + resultModel.getName());
for (Field resultField : resultModel.getFields()) {
System.out.println("\tField: " + resultField.getId() + " " + resultField.getName());
}
}
Which results in the following, your expected output:
Model: modelId1 Model1
Field: fieldId1 Field1
Field: fieldId2 Field2
Result - graphical representation
I hope that this information proves helpful. Please don't hesitate to ask any unclear items.

Neo4J, Spring Data. How to query Relationship entity?

I use Neo4J database with Spring Data. I am unable to query (with custom query) a relationship directly to my Relation entity which looks like that:
#RelationshipEntity(type = "OCCURS_WITH")
public class Relation {
#GraphId
private Long id;
#StartNode
#Fetch
private Hashtag from;
#EndNode
#Fetch
private Hashtag to;
#GraphProperty(propertyType = long.class)
private Long[] timestamps = new Long[0];
private boolean active;
// getters, setters
}
I have also a repository interface as follow:
public interface RelationRepository extends CRUDRepository<Relation> {
#Query(value = " MATCH (h1)-[rel]->(h2) " +
" WHERE h1.name = {0} AND h2.name = {1}" +
" RETURN rel")
Relation find(String from, String to);
}
But when I query the repository I get an empty Relation object.
Everything works well when I am quering to dummy object in that way:
#Query(value = " MATCH (h1)-[r]->(h2) " +
" WHERE h1.name = {0} AND h2.name = {1} " +
" RETURN id(r) AS id, h1.name AS from, h2.name AS to, length(r.timestamps) AS size")
RelationshipData findData(String from, String to);
#QueryResult
public interface RelationshipData {
#ResultColumn("id")
String getId();
#ResultColumn("from")
String getFrom();
#ResultColumn("to")
String getTo();
#ResultColumn("size")
int getSize();
}
Is it possible to query directly to my entity?

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to iland.hbm.BillDetails`

Following are my code where I am fetching data from multiple table and want to print that data.
For fetching data I am using following method
public List<BillDetails> fetch(long id, long cid) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
List<BillDetails> obj = null;
try {
String hql = "select distinct bd,sum(bpds.amount) from BillDetails as bd "
+ "left join fetch bd.customerDetails as cd "
+ "left join fetch bd.billProductSet as bpd "
+ "left join fetch bpd.product as pd "
+ "left join fetch bd.billPaidDetails as bpds "
+ "where bd.billNo=:id "
+ "and bd.client.id=:cid ";
Query query = session.createQuery(hql);
query.setParameter("id", id);
query.setParameter("cid", cid);
obj = query.list();
tx.commit();
} catch (HibernateException e) {
if (tx != null) {
e.printStackTrace();
tx.rollback();
}
} finally {
session.close();
}
System.out.println("Size is " + obj.size());
System.out.println(" " + obj.get(0).getBillNo());
return obj;
}
BillDetails pojo
public class BillDetails implements java.io.Serializable {
private Long billNo;
private CustomerDetails customerDetails;
private Client client;
private BigDecimal subTotal;
private BigDecimal vat;
private BigDecimal total;
private String invoiceNo;
private Date invoiceDate;
private String status;
private Timestamp addDate;
private Set<BillPaidDetails> billPaidDetails = new HashSet(0);
private Set<BillProduct> billProductSet = new HashSet(0);
//getter and setter
}
BillPaidDetails
public class BillPaidDetails implements java.io.Serializable {
private Long id;
private Client client;
private BillDetails billDetails;
private BigDecimal amount;
private String paymentMode;
private Date dt;
private Timestamp adddate;
}
While printing value at fetch() at System.out.println(" " + obj.get(0).getBillNo());
It is showing
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to iland.hbm.BillDetails.
How to resolve above exception and where the sum(bpds.amount) will store.
Is it possible that sum(bpds.amount) should be store in any desired fields of BillPaidDetails like added a field private BigDecimal totalAmount; where sum will stored.
First of all this query can't work as it is. I already responded about the need for including all fetched entities' properties in the group by clause.
You try to select both an entity and a sum, while expecting to fit into a List<BillDetails>.
If you were only selecting BillDetails you'd be fine. But when you want to select more than one entity you need to assign the select to a List<Object[]>:
List<Object[]> obj = null;
...
Object[] bdAndSum = obj.get(0);
BillDetails bd = (BillDetails) bdAndSum.get(0);
Number sum = (Number) bdAndSum.get(1);

play framework scala get join table attribute

I am using Play framework with Ebean. I have two models, below are the code:
public class User extends Model {
#Id
public Long id;
public String name;
/* rest of attributes */
public static Finder<Long,User> find = new Finder<Long,User>(
Long.class, User.class
);
}
public class Admin extends Model {
#Id
public Long id;
#OneToOne
public User user;
/* rest of attributes */
public static Finder<Long,Admin> find = new Finder<Long,Admin>(
Long.class, Admin.class
);
}
When I do Logger.info(admin.user.name) in Java, I can see the name of the admin. But when I pass the Java object to Scala using view render, if I do #admin.user.id, I can still get the id, but if I do #admin.user.name, I get nothing (with no error). I'm just wonder how can I access the name attribute from a joined table?
Problem solved.
Before when I do the fetching, I did
Admin.find.where()
.ilike("user.name", "%" + filter + "%")
.orderBy("user."+sortBy + " " + order)
.findPagingList(pageSize)
.getPage(page);
After changing to
Admin.find.fetch("user", new FetchConfig().query())
.where()
.ilike("user.name", "%" + filter + "%")
.orderBy("user."+sortBy + " " + order)
.findPagingList(pageSize)
.getPage(page);
It successfully displayed instance variables on Scala.

Categories

Resources