Need help On hibernate


#1

I am running the below code using the query but the problem here is it is fetching the total count(Number of records in a table) but if i am printing the result it giving the same output.

List<ColumnDetails> demoEntitities = (List<ColumnDetails>)session.createQuery("from ColumnDetails where ParentID = '28948811' and TRUNC(ModifyDate) = TRUNC (SYSDATE)-1").list();
		   
for(int j=0; j<demoEntitities.size(); j++) {
System.out.println("Wsw");
	Map<String, String> map = null;
	demoEntitities.get(j);
	//System.out.println(list.get(j));
	map = (Map<String, String>) demoEntitities.get(j);
	String NodeID =  String.valueOf(map.get("DataID"));
	String NameOfTheDocument =  String.valueOf(map.get("Name"));
	//System.out.println("NodeID is "  +NodeID);
	System.out.println("DocumentName is "  +NameOfTheDocument);
}

for the above query result : getting count as 66 (correct)
The below output getting for 66 times.

DocumentName is 14_gm.gif
Wsw
DocumentName is 14_gm.gif
Wsw
DocumentName is 14_gm.gif
Wsw
DocumentName is 14_gm.gif
Wsw
DocumentName is 14_gm.gif
Wsw
DocumentName is 14_gm.gif

#2

It’s not clear what you are asking. Why do you mean by count(Number of records in a table)? There’s no count query. You are just selecting some entities.

Try to read your post from the perspective of someone who has no idea what you are doing and change it to be clear.


#3

The total records in the table is 66.

When I am fetching those 66 records it’s giving me the same result.
If it is not clear let me know so that I will explain in clear


#4

The whole code does not make any sense.

Map<String, String> map = null; //That's useless.
demoEntitities.get(j); //That's even more useless.

Now, here comes the impossible part:

 map = (Map<String, String>) demoEntitities.get(j);

How can the demoEntitities List contains Map entries? You declared it as List<ColumnDetails>.

System.out.println("DocumentName is "  +NameOfTheDocument);

You should use a Logging framework even for simple tests like this one.

Just override toString in ColumnDetails and you can rewrite your test case like this:

List<ColumnDetails> demoEntitities = (List<ColumnDetails>) session.createQuery(
	"from ColumnDetails where ParentID = '28948811' and TRUNC(ModifyDate) = TRUNC (SYSDATE)-1")
.getResultList();
		   
for(ColumnDetails columnDetails : demoEntitities) {
	log.info("ColumnDetails: {}", columnDetails);
}

#5

When i am getting error with “log.info”, please check the image attached. how and to get the values from that object.


#6

while i am executing that code i am getting this error as well.


#7

Either you upgrade to Hibernate 5.3 or you just use the Hibernate Query interface instead of the JPA one from the javax.persistence package.


#8

Can you explain in detail so that i will resolve the issue


#9

If you read the User Guide, you will know how to fix more than this issue.