generated aliases error in spring JPA in criteria query API - spring-boot

public Predicate get_data(String field, String method, String value) throws Exception
{
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<student> cq = cb.createQuery(student.class);
Root<student> s = cq.from(student.class);
Predicate selected_field = null;
number = new BigDecimal(value);
if(method.equals("greaterThan") || method.equals("greaterThanOrEqualTo") ||
method.equals("lessThan") || method.equals("lessThanOrEqualTo"))
{
Method cbMethod = cb.getClass().getMethod(method,Expression.class,Comparable.class);
selected_field = (Predicate) cbMethod.invoke(cb,s.get(field), number);
}
else if(method.equals("eq"))
{
method = "equal";
Method cbMethod = cb.getClass().getMethod(method,Expression.class,Object.class);
selected_field = (Predicate) cbMethod.invoke(cb,s.get(field),number);
}
return selected_field;
}
public List<student> get(String search) throws Exception
{
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<student> cq = cb.createQuery(student.class);
Root<student> s = cq.from(student.class);
List<Predicate> selected_field = new ArrayList<>();
System.out.println(search);
List<String> query = Arrays.asList(search.split("And",0));
System.out.println(query.size());
List<String> fields = new ArrayList<>();
List<String> values = new ArrayList<>();
List<String> method = new ArrayList<>();
for (String str : query)
{
List<String> s1 = null;
int index_lt = str.indexOf('<');
int index_gt = str.indexOf('>');
int index_eq = str.indexOf('=');
if(index_lt != -1)
{
s1 = Arrays.asList(str.split("<",0));
method.add("lessThan");
}
else if(index_gt != -1)
{
s1 = Arrays.asList(str.split(">",0));
method.add("greaterThan");
}
else if(index_eq != -1)
{
s1 = Arrays.asList(str.split("=",0));
method.add("equal");
}
System.out.println("S1"+s1);
fields.add("student_"+s1.get(0));
values.add(s1.get(1));
}
for(int i = 0 ; i < fields.size() ; i++)
{
selected_field.add(get_data(fields.get(i),method.get(i),values.get(i)));
}
cq.where(cb.and(selected_field.toArray(new Predicate[] {})));
cq.where(predicatesarr);
System.out.println("where ends");
TypedQuery<student> search_query = em.createQuery(cq);
return search_query.getResultList();
}
when I am sending this http://localhost:8080/?search=rollNo>13 then it is giving me this error.
org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path: 'generatedAlias1.student_rollNo' [select generatedAlias0 from io.poc_task1.poc_task1.student as generatedAlias0 where generatedAlias1.student_rollNo>13]
Can anyone help me with this? Thanks in advance.

Related

Go through sub tags of IsoMsg and get values for jPOS

I have a jPOS project and I want to loop through sub tags of a field to get it's values. I have the following code and I don't know if it does the job:
Map<String, String> subTagMap = new HashMap<>();
ISOMsg champ11 = (ISOMsg)isoMsg.getComponent(11);
int i = 1;
while(i < 15) {
if (champ11 != null) {
ISOTaggedField subtTag = findTag(champ11, (i < 10) ? "0" + i : String.valueOf(i));
if (subtTag != null) {
LOGGER.debug("Champ11x" + i +"trouvé");
subTagMap.put(subtTag.getTag(), subtTag.getValue().toString());
}
}
i++;
}
return subTagMap;
}
private ISOTaggedField findTag(ISOComponent component, String tagName) {
if (component instanceof ISOTaggedField) {
component = ((ISOTaggedField) component).getDelegate();
}
#SuppressWarnings("unchecked")
Map<Integer, Object> children = component.getChildren();
for (Map.Entry<Integer, Object> entry : children.entrySet()) {
if (entry.getValue() instanceof ISOTaggedField) {
ISOTaggedField tag = (ISOTaggedField) entry.getValue();
if (tag.getTag().equals(tagName)) {
return tag;
}
}
}
return null;
}
This should work:
Map<String, String> subTagMap=new HashMap<>();
ISOMsg champ11 = (ISOMsg) isoMsg.getComponent(119);
if (champ != null) {
for (Object v : champ.getChildren().values()) {
if (!(v instanceof ISOTaggedField)) continue;
ISOTaggedField subTag = (ISOTaggedField) v;
subTagMap.put(subTag.getTag(), (String)subTag.getValue());
}
}
return subTagMap;

Fix to assigning value to anonymous type

In the query below I created new variables with anonymous type.
var sampleDetailsList = (from detailsA in context.SampleDetailsA
join detailsB in context.SampleDetailsB
on
new
{
Key1 = detailsA.SampleId,
Key2 = detailsA.Main.Year
}
equals
new
{
Key1 = detailsB.SampleId,
Key2 = detailsB.Main.Year
}
where (detailsA.Main.Year == "2018" && detailsB.Main.Year == "2018")
select new
{
SDASampleId = detailsA.SDASampleId,
SDASampleDetailId = detailsA.Id,
SDAAmountA = detailsA.SDAAmountA,
SDAAmountB = detailsA.SDAAmountB,
SDAAmountC = detailsA.SDAAmountC,
SDBSampleId = detailsB.SDBSampleId,
SDBSampleDetailId = detailsB.Id,
SDBAmountA = detailsB.SDBAmountA,
SDBAmountB = detailsB.SDBAmountB,
SDBAmountC = detailsB.SDBAmountC,
}).ToList();
Since anonymous type's properties are read-only. I could not assign new values in foreach loop. I need to add the amount in the transactional list below to the sampleDetailsList if a condition is met.
var transactionalList = (from tra in context.Transactions
where (tra.Year == "2018") && (tra.SDASampleDetailId != null || tra.SDBSampleDetailId != null)
select tra).ToList();
if (transactionalList.Count != 0)
{
foreach (var traItem in transactionalList)
{
foreach (var smDetail in sampleDetailsList)
{
if (smDetail.SDASampleId == traItem.SDASampleId)
{
if (traItem.TypeId == "AAA")
{
smDetail.SDAAmountA = smDetail.SDAAmountA + traItem.Amount;
}
else if (traItem.TypeId == "BBB")
{
smDetail.SDAAmountB = smDetail.SDAAmountB + traItem.Amount;
}
else if (traItem.TypeId == "CCC")
{
smDetail.SDAAmountC = smDetail.SDAAmountC + traItem.Amount;
}
}else if (smDetail.SDBSampleId == traItem.SDBSampleId)
{
if (traItem.TypeId == "AAA")
{
smDetail.SDBAmountA = smDetail.SDBAmountA + traItem.Amount;
}
else if (traItem.TypeId == "BBB")
{
smDetail.SDBAmountB = smDetail.SDBAmountB + traItem.Amount;
}
else if (traItem.TypeId == "CCC")
{
smDetail.SDBAmountC = smDetail.SDBAmountC + traItem.Amount;
}
}
}
}
}
How to assign the values just like in the foreach loop?
It seems to me that your query is like this:
var results =
(
from detailsA in context.SampleDetailsA
join detailsB in context.SampleDetailsB
on
new
{
Key1 = detailsA.SampleId,
Key2 = detailsA.Main.Year
}
equals
new
{
Key1 = detailsB.SampleId,
Key2 = detailsB.Main.Year
}
where detailsA.Main.Year == "2018"
where detailsB.Main.Year == "2018"
from tra in context.Transactions
where tra.Year == "2018"
where (tra.SDASampleDetailId != null || tra.SDBSampleDetailId != null)
select new
{
SDASampleId = detailsA.SDASampleId,
SDASampleDetailId = detailsA.Id,
SDAAmountA = detailsA.SDAAmountA + (((detailsA.SDASampleId == tra.SDASampleId) && tra.TypeId == "AAA") ? tra.Amount : 0),
SDAAmountB = detailsA.SDAAmountB + (((detailsA.SDASampleId == tra.SDASampleId) && tra.TypeId == "BBB") ? tra.Amount : 0),
SDAAmountC = detailsA.SDAAmountC + (((detailsA.SDASampleId == tra.SDASampleId) && tra.TypeId == "CCC") ? tra.Amount : 0),
SDBSampleId = detailsB.SDBSampleId,
SDBSampleDetailId = detailsB.Id,
SDBAmountA = detailsB.SDBAmountA + (((detailsB.SDBSampleId == tra.SDBSampleId) && tra.TypeId == "AAA") ? tra.Amount : 0),
SDBAmountB = detailsB.SDBAmountB + (((detailsB.SDBSampleId == tra.SDBSampleId) && tra.TypeId == "BBB") ? tra.Amount : 0),
SDBAmountC = detailsB.SDBAmountC + (((detailsB.SDBSampleId == tra.SDBSampleId) && tra.TypeId == "CCC") ? tra.Amount : 0),
}
)
.ToList();
And just to help others, this is the code that I had to write to make this code compilable:
public static class context
{
public static List<DetailA> SampleDetailsA = new List<DetailA>();
public static List<DetailB> SampleDetailsB = new List<DetailB>();
public static List<Transaction> Transactions = new List<Transaction>();
}
public class Transaction
{
public string Year;
public int? SDASampleDetailId;
public int? SDBSampleDetailId;
public int SDASampleId;
public int SDBSampleId;
public string TypeId;
public int Amount;
}
public class DetailA
{
public int Id;
public int SampleId;
public int SDASampleId;
public int SDAAmountA;
public int SDAAmountB;
public int SDAAmountC;
public Main Main;
}
public class DetailB
{
public int Id;
public int SampleId;
public Main Main;
public int SDBSampleId;
public int SDBAmountA;
public int SDBAmountB;
public int SDBAmountC;
}
public class Main
{
public string Year;
}
The only way out is probably creating a class SampleDetail with the properties that you need, and then change the LINQ to use select new SampleDetail { SDASampleId = ... etc }.
You can then change the values of each SampleDetail object as needed, at any time afterwards.

redisTemplate ValueOperations increment get null

First of all, I have to say that key exists in redis,
This key is a self-increasing sequence,
But sometimes getting this key is null.
I used spring-boot: 2.0.4.0.release
My expectation is to get a self-increment of 1 at a time to generate the order number
Could you advice a solution for my scenario? Thanks!
#Override
#Transactional(rollbackFor = Exception.class)
public boolean paymentBatchMeter(UserDescriptor user, String areaCode, String newPrice) {
List<Meter> meters = meterService.findAllByFilterAndAreaCode(null, areaCode, user);
if (meters != null && meters.size() > 0) {
List<TaskValue> taskValueList = new ArrayList<>();
meters.forEach(meter -> {
if (meter.getState() == 1) {
Task task = new Task();
task.setCode(meter.getCode());
TaskValue taskValue = new TaskValue();
taskValue.setId(task.getId());
taskValue.setMeterType(meter.getMeterDicCode());
taskValue.setCode(meter.getCode());
Long orderIndex = redisTemplate.opsForValue().increment("athena:order:index", 1);
if (orderIndex == null) {
throw new IllegalStateException("error!");
}
taskValueList.add(taskValue);
Order order = new Order();
order.setCustomerId(meter.getCustomerId());
order.setCode(orderIndex.toString());
order.setCreateTime(new Date());
order.setMeterId(meter.getId());
order.setState(0);
order.setRechargeMoney(new BigDecimal(newPrice));
List<DicProjectItem> dicProjectItems = dicProjectItemMapper.getDicProjectItemAll("D10015", 1);
String dicProjectItemId = dicProjectItems.stream().filter(p -> "002".equals(p.getCode())).collect(Collectors.toList()).get(0).getId();
order.setRechargeWayDicId(dicProjectItemId);
order.setRechargeState(1);
order.setTaskId(task.getId());
order.setOperatorId(user.getId());
}
});
redisTemplate.setEnableTransactionSupport(true);
taskValueList.forEach(taskValue -> {
if ("004".equals(taskValue.getMeterType())) {
redisTemplate.convertAndSend(TOPIC, taskValue.getCode());
redisTemplate.opsForList().rightPush("athena:concentrator:meter:task:" + taskValue.getCode(), JSON.toJSONString(taskValue));
} else {
redisTemplate.opsForList().rightPush("athena:meter:task:" + taskValue.getCode(), JSON.toJSONString(taskValue));
}
});
redisTemplate.setEnableTransactionSupport(false);
return true;
}
return false;
}

JdbcTemplate is returning empty result

could you help me previously its giving the perfect result but now its not give me
private String SQL_SERVICE_CONFIGS1 = "SELECT cc.client, ac.association_code, as1.service_name, as1.service_type, as1.service_implementation, as1.endpoint_url FROM association_config ac, association_services as1,client_configs cc WHERE as1.association_id=ac.id AND cc.id=ac.client_config_id AND ac.association_code= ?";
public List<AssociationConfiguration> getAssociationConfigurations(String clientId, String associationId) {
String sql = SQL_SERVICE_CONFIGS1;
Object[] args = new Object[] {};
if (associationId != null && associationId.trim().length() > 0) {
args = new Object[] { associationId.trim() };
}
return (List<AssociationConfiguration>) jdbcTemplate.query(sql, args,
new AssociationConfigurationRowMapper());
}
}

Can't apply Geo queries in parse (Parse Framework)

I have tried to apply the geopoints in parse but I can't do the query. The code is as follows.
ParseObject placeObject = new ParseObject("placeObject");
ParseGeoPoint userLocation = (ParseGeoPoint) placeObject.get("location");
ParseQuery query = new ParseQuery("PlaceObject");
query.whereNear("location", userLocation);
query.setLimit(10);
query.findInBackground(new FindCallback() {
public void done(List<ParseObject> Locaciones, ParseException e) {
if (e == null) {
Log.d("ERROR",Locaciones.toString());
// object will be your game score
} else {
Log.d("ERROR",e.toString());
// something went wrong
}
}
});
Try this:
ParseObject.registerSubclass(MasterEntity.class);
ParseGeoPoint userLocation = new ParseGeoPoint(latitude,longitude);
ParseQuery<MasterEntity> query = ParseQuery.getQuery("UserMaster");
query.whereNear("location", userLocation);
query.setLimit(30);
query.findInBackground(new FindCallback<MasterEntity>() {
#Override
public void done(List<MasterEntity> mlist, ParseException e) {
dlg.dismiss();
if (e==null) {
for (int i = 0; i < mlist.size(); i++) {
MasterEntity cc = mlist.get(i);
String s_email = cc.getEmail();
String s_uname = cc.getuserName();
String s_objid = cc.getObjectID();
List<ParseObject> aryFollower = cc.getFollowers();
List<ParseObject> aryFollowing = cc.getFollowing();
}
}
else
{
Alert.alertOneBtn(getActivity(),"Something went wrong.Please try again later");
}
}
});
Sample App : https://github.com/ParsePlatform/AnyWall/tree/master/AnyWall-android
Tutorial : https://parse.com/tutorials/anywall-android

Resources