jpql

Create custom JPA query function

妖精的绣舞 提交于 2020-01-06 05:46:04
问题 I'm using EclipseLink. Is is possible to use custom query function like: Select u from User u where my_function(u.name) = my_function(:param) I would like to retrive entities based on custom created slug names rather than id (and remove any id's from url) 回答1: If you implement it as function in database then it is possible, otherwise not (because it translates to SQL in the end anyway calls to method implemented with Java are not possible). Keyword func is used to call database function:

JPQL create table at runtime?

走远了吗. 提交于 2020-01-05 12:29:18
问题 Is there possible way to create the table at runtime without using the native query. Point : if native query is used, SQL query may be depend on the DB server. Otherwise, is there possible way to create a table at runtime. I used EclipseLink 2.4.1 and Spring 3.1.1 . 回答1: As per JPQL specification, there is no way to create new tables or entities. JPQL is run on entities which would have been bound to schema already and so, it seems logical to be not able to create tables on the fly. 来源: https

Select Distinct * as query method?

大兔子大兔子 提交于 2020-01-05 05:39:16
问题 In my Spring project, I'm currently using only query methods. Now, when calling findAll(Pageable) with a pageable that contains a sort of a collection property, I'm experiencing a known and expected issue: DATAJPA-744: duplicate results when sorting by collection property An easy way and also suggested way to solve this is by using the DISTINCT keyword to filter the result. My problem is that when I create the repository method findDistinct , spring throws an exception on initializing telling

JPQL Group By TimeStamp as a Date

穿精又带淫゛_ 提交于 2020-01-05 02:24:12
问题 In a JPA application the Bill entity has an attribute named createAt with a TimeStamp annotation. I want to group Bills by createdAt by Date only, not by TimeStamp. In the query, although I pass TemporalType as Date, still bills are grouped as Time. How can I group Bills by date using the createdAt attribute. I use JPA with EclipseLink 2.5. The Bill Entity @Entity public class Bill implements Serializable { .... .... @Temporal(javax.persistence.TemporalType.TIMESTAMP) Date createdAt; .... ...

How to build JPQL queries when parameters are dynamic?

好久不见. 提交于 2020-01-04 09:21:52
问题 I wonder if there is a good solution to build a JPQL query (my query is too "expressive" and i cannot use Criteria) based on a filter. Something like: query = "Select from Ent" if(parameter!=null){ query += "WHERE field=:parameter" } if(parameter2!=null) { query += "WHERE field2=:parameter2" } But i would write WHERE twice!! and the casuistic explodes as the number of parameter increases. Because none or all could be null eventually. Any hint to build these queries based on filters on a

hql join @CollectionTable

北城余情 提交于 2020-01-03 20:04:35
问题 I have a domain Service with collection tags as below : @Entity public class Service extends AbstractEntity<Long> { private static final long serialVersionUID = 9116959642944725990L; @ElementCollection(fetch = FetchType.EAGER, targetClass = java.lang.String.class) @CollectionTable(name = "service_tags", joinColumns = @JoinColumn(name = "s_id")) @Column(name = "tag") private Set<String> tags; } I want to select Service s with particular KEY of Service.tags . hql joining Service to Service.tags

How to register non-standarized SQL functions manually in Spring Boot application?

本秂侑毒 提交于 2020-01-03 03:08:13
问题 I'm using JPA query in my current spring-boot project. How can I add non-standardized SQL functions like GROUP_CONCAT ? Prior, to my previous problem : How to show a column result in a one line comma separated list in JPA query I found that GROUP_CONCAT is not a registered function in JPA query but could be accessed by registering it manually. I already tried following links but didn't work for me : How to add non-standardized sql functions in Spring Boot application? Registering a SQL

Indexed element access in JPQL

拟墨画扇 提交于 2020-01-02 08:09:12
问题 Is it possible to do indexed element access in JPQL like in HQL : select o from Order o where o.items[0].id = 1234 I couldn't find something related in the JPA 2 specs, I am targeting EclipseLink JPA here, so if you come up with an EclipseLink solution, that's ok as well, although a JPQL standard solution is preferred. 回答1: The INDEX function should do the trick (actually I tested it and it does): SELECT o FROM Order o JOIN o.items i WHERE i.id = 1234 AND INDEX(i) = 0 From the JPA 2.0

Using JPA AttributeConverter for Boolean Y/N field: “Unable to render boolean literal value”

£可爱£侵袭症+ 提交于 2020-01-02 07:45:32
问题 I'm implementing the solution here to convert a 'Y'/'N' column to a Boolean value: @Basic(optional = false) @Column(name = "ACTIVE_YN") @Convert(converter = BooleanToStringConverter.class) private Boolean active; .. and: @Converter public class BooleanToStringConverter implements AttributeConverter<Boolean, String> { @Override public String convertToDatabaseColumn(Boolean value) { return (value != null && value) ? "Y" : "N"; } @Override public Boolean convertToEntityAttribute(String value) {

How to deal with Object type returned by JPQL query?

泄露秘密 提交于 2020-01-02 04:06:07
问题 I can do this easily with JPQL that only returns data from one table. SELECT m1 FROM MasatosanTest m1 That means one data type is returned. So I can just store the query result into the List with type specified: List<MasatosanTest> mt = query.getResultList(); code snippet private static final String JPQL_TEST = "SELECT m1 FROM MasatosanTest m1; @Path("innerJoin") @GET @Produces("application/json") public List<MasatosanTest> getJoinedResult() { System.out.println("getJoinedResult called");