How to improve the intuitivity

I am using a LINQ style way to write HQL-Queries. There are other solutions but they bring frameworks I do not like to use. Today I have a good working solution that abuse the unicode charset so the sourcecode must be at least UTF8. My solution is working but I am not sure if this is a good solution for the general public. What to change to the syntax in order to publish it to the public accordingly? Example: We have two Entities: eMail and eMailSubject. A eMail already exists as an hibernate-entity. The function I present is to programaticaly response to that eMail. The task is to add the "Re: "-prefix to the subject. Code:
EMail answer = new EMail();
String targetSubject = "Re: " + original.getSubject().getSubject();
answer.setSubject(SELECT.FROM(囗EMailSubject.class).AS(ES -> WHERE(ES.subject).ㅤᆖㅤ(targetSubject).SELECT().ꁘ(ES))
.first(entityManager, () -> new EMailSubject(targetSubject)));
entityManager.persist(answer);
return ResponseEntity.ok(answer.getId());
EMail answer = new EMail();
String targetSubject = "Re: " + original.getSubject().getSubject();
answer.setSubject(SELECT.FROM(囗EMailSubject.class).AS(ES -> WHERE(ES.subject).ㅤᆖㅤ(targetSubject).SELECT().ꁘ(ES))
.first(entityManager, () -> new EMailSubject(targetSubject)));
entityManager.persist(answer);
return ResponseEntity.ok(answer.getId());
21 Replies
JavaBot
JavaBot2w ago
This post has been reserved for your question.
Hey @Peter Rader! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Peter Rader
Peter RaderOP2w ago
Lets break it down to the components:
EMail answer = new EMail();
String targetSubject = "Re: " + original.getSubject().getSubject();
Aliasing<EMailSubject, 囗EMailSubject> aliasScope = SELECT.FROM(囗EMailSubject.class);
Function<囗EMailSubject, HQLQueryContributor> whereFunction = ES -> WHERE(ES.subject).ㅤᆖㅤ(targetSubject).SELECT()
.ꁘ(ES);
HQLQueryContributor queryBuilder = aliasScope.AS(whereFunction);
Supplier<EMailSubject> noResultPersistFallback = () -> new EMailSubject(targetSubject);
answer.setSubject(queryBuilder.first(entityManager, noResultPersistFallback));
entityManager.persist(answer);
return ResponseEntity.ok(answer.getId());
EMail answer = new EMail();
String targetSubject = "Re: " + original.getSubject().getSubject();
Aliasing<EMailSubject, 囗EMailSubject> aliasScope = SELECT.FROM(囗EMailSubject.class);
Function<囗EMailSubject, HQLQueryContributor> whereFunction = ES -> WHERE(ES.subject).ㅤᆖㅤ(targetSubject).SELECT()
.ꁘ(ES);
HQLQueryContributor queryBuilder = aliasScope.AS(whereFunction);
Supplier<EMailSubject> noResultPersistFallback = () -> new EMailSubject(targetSubject);
answer.setSubject(queryBuilder.first(entityManager, noResultPersistFallback));
entityManager.persist(answer);
return ResponseEntity.ok(answer.getId());
The benefit is that you have full content-assist in IDEs and you need no additional dependency. The downside is that the sourcecode must be unicode in order to be used correctly. What must be changed in order to present the solution to the public?
dan1st
dan1st2w ago
Why do you use special unicode characters there? Can't you just use underscores or similar? There is already the approach of type-safe queries with the Jakarta Criteria API metamodel classes (classes that end with an _ and are generated at compile-time) see https://jakarta.ee/learn/docs/jakartaee-tutorial/current/persist/persistence-criteria/persistence-criteria.html#_using_the_metamodel_api_to_model_entity_classes - you might want to take inspiration from that ;) Also how do you get from the data/entity classes to the generated classes? Are you using annotations on the entities (or however you want to call them)? Are you using an annotation processor to generate these classes?
Peter Rader
Peter RaderOP2w ago
I like to use a unicode-pendant for the equals-sign (=), it is a control-character and not allowed as a method-name. Yes at the moment. I will get rid of generating the intermediate-code due to new language improvements that did not exists before. Example: BiFunction<EMail, EMailSubject, EMail> subjectSetter = EMail::setSubject;
dan1st
dan1st2w ago
and this is what it looks like in Discord
No description
dan1st
dan1st2w ago
What do you mean with didn't exist before? Method references were available in JDK 8 already... Are you talking about code reflection? Because that would allow you to do it in a type-safe way without generating that code at compile-time
Peter Rader
Peter RaderOP2w ago
I admit that it is an old solution.
dan1st
dan1st2w ago
Is there an issue with it being legal method declarations that nobody will use?
Peter Rader
Peter RaderOP2w ago
You mean code-reflection like JpaRepositories? I think I did not made this point clear. If you have a java-method like this: public static void =(String param) {} the compiler will mention that = is not a valid method name.
dan1st
dan1st2w ago
Code reflection is a planned Java/JVM feature that should allow Java code to look into a method. So you could do
Query<EMailSubject> query = Query.create(EMailSubject.class);
query.where(subject -> subject.getAddress().equals("[email protected]"));
Query<EMailSubject> query = Query.create(EMailSubject.class);
query.where(subject -> subject.getAddress().equals("[email protected]"));
and then it could actually analyze that and convert it to SQL (without having to look at the bytecode) and it would also be useful for autodifferentiation and GPU stuff but it isn't there yet
Peter Rader
Peter RaderOP2w ago
Nice feature but its a runtime feature, right? Id like to focus on the IDE's content-assist.
Madjosz
Madjosz2w ago
Do you have a JEP at hand for that?
dan1st
dan1st2w ago
There's no JEP yet just talks and I think design documents it's far from being done
Madjosz
Madjosz2w ago
Ok, so JDK 40+ feature then. Like Valhalla.
dan1st
dan1st2w ago
Java
YouTube
Project Babylon - Code Reflection #JVMLS
Presented by Paul Sandoz - Software Architect (Java Platform Group - Oracle) during the JVM Language Summit (August 2024 - Santa Clara, CA). _Project Babylon aims to extend the reach of Java to foreign programming models with an enhancement to Java reflection called code reflection . Code reflection expands reflective access to ...
dan1st
dan1st2w ago
There are relevant JEPs for that
Madjosz
Madjosz2w ago
Yeah, they are slowly adding features for that
dan1st
dan1st2w ago
babylon is fairly new as an OpenJDK project but it won't be as big in scope as Valhalla https://openjdk.org/projects/babylon/ That one would be relevant to this question: https://openjdk.org/projects/babylon/articles/linq
Peter Rader
Peter RaderOP2w ago
About the code-reflection... I remember a discussion with a collegue who like to write a unit-test to make sure a constructor is a pojo-constructor and no other magic happens there, I suggested to ASM-disassemble and check the bytecode-instructions.... The discussion ended with the decision to not unit-test bytecode.
dan1st
dan1st2w ago
good idea the better way to do that would probably be the AST API (since you have the source code) and probably do it at compile-time with an annotation processor or similar
JavaBot
JavaBot2w ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?