hql

Spring data jpa. Find max if no result return default value

佐手、 提交于 2019-12-03 06:15:51
I've implemented in my spring repository interface: @Query("SELECT max(ch.id) FROM MyEntity ch") Long getMaxId(); It works correctly if db is not empty. If I start my environment with test configuration (H2DB is used) - there is no data in the very beginning. And result returned by getMaxId() is null . I would like to have here 0 . Is it possible to modify my *JpaRepository to have 0 result? If yes, how it should be modified? You can use coalesce like : @Query("SELECT coalesce(max(ch.id), 0) FROM MyEntity ch") Long getMaxId(); If there are no data it will return 0 instead of null. 来源: https:/

[Ljava.lang.Object; cannot be cast to

北城以北 提交于 2019-12-03 05:49:04
问题 I want to get value from the database, in my case I use List to get the value from the database but I got this error Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to id.co.bni.switcherservice.model.SwitcherServiceSource at id.co.bni.switcherservice.controller.SwitcherServiceController.LoadData(SwitcherServiceController.java:48) at id.co.bni.switcherservice.controller.SwitcherServiceController.main(SwitcherServiceController.java:62) this is my code

IFNULL equivalent in Hibernate Query Language?

喜夏-厌秋 提交于 2019-12-03 05:43:56
I'm trying to write an HQL query which will calculate an average rating for an item. I want the query to return 0 instead of null when there are no rating for a given item - so that I can use my query as a subquery. So is it possible? Is there an HQL equivalent of IFNULL or NVL? COALESCE is the official equivalent. It returns the first non-null of its arguments. Example: COALESCE(id_pati, 0) Link Wikipedia Nhibernate docs are out of date. Check http://docs.jboss.org/hibernate/stable/core/reference/en/html/queryhql.html If nothing works you can try: select case when something is not NULL then 0

Hibernate detached queries as a part of the criteria query

走远了吗. 提交于 2019-12-03 05:20:09
问题 java experts can you please help me write detached queries as a part of the criteria query for the following SQL statement. select A.* FROM AETABLE A where not exists ( select entryid FROM AETABLE B where B.classpk = A.classpk and B.userid = A.userid and B.modifiedDate > A.modifiedDate ) and userid = 10146 回答1: You need to write a correlated subquery. Assuming property / class names match column / table names above: DetachedCriteria subquery = DetachedCriteria.forClass(AETable.class, "b")

hibernate fetch属性

我怕爱的太早我们不能终老 提交于 2019-12-03 04:25:55
Hibernate的fetch="join"和fetch="select" 的一点分析 fetch参数指定了关联对象抓取的方式是select查询还是join查询,select方式时先查询返回要查询的主体对象(列表),再根据关联外键id,每一个对象发一个select查询,获取关联的对象,形成n+1次查询; 而join方式,主体对象和关联对象用一句外键关联的sql同时查询出来,不会形成多次查询。 如果你的关联对象是延迟加载的,它当然不会去查询关联对象。 另外,在hql查询中配置文件中设置的join方式是不起作用的(而在所有其他查询方式如get、criteria或再关联获取等等都是有效的),会使用select方式,除非你在hql中指定join fetch某个关联对象。 fetch策略用于定义 get/load一个对象时,如何获取非lazy的对象/集合。 这些参数在Query中无效。 fetch策略用于定义 get/load一个对象时,如何获取非lazy的对象/集合。 这些参数在Query中无效。 查询抓取(默认的)在N+1查询的情况下是不好的,因此我们可能会要求在映射文档中定义使用连接抓取: 在映射文档中定义的抓取策略将会有产生以下影响: 通过get()或load()方法取得数据。 只有在关联之间进行导航时,才会隐式的取得数据(延迟抓取)。 条件查询 在映射文档中显式的声明

Hibernate is doing multiple select requests instead one (using join fetch)

强颜欢笑 提交于 2019-12-03 03:51:24
问题 I've got the following query which I expect to run in a single select request: @NamedQuery(name=Game.GET_GAME_BY_ID1, query = "SELECT g FROM Game g " + "JOIN FETCH g.team1 t1 " + "JOIN FETCH t1.players p1 " + "JOIN FETCH p1.playerSkill skill1 " + "where g.id=:id") The problem is that everything is fetched by separate multiple queries. I want only Team and team's players and each player's skills to be fetched in a single request. But instead I've got multiple select queries for fetching each

How to dynamically order many-to-many relationship with JPA or HQL?

て烟熏妆下的殇ゞ 提交于 2019-12-03 03:29:13
I have a mapping like this: @ManyToMany(cascade = CascadeType.PERSIST) @JoinTable( name="product_product_catalog", joinColumns={@JoinColumn(name="product_catalog", referencedColumnName="product_catalog")}, inverseJoinColumns={@JoinColumn(name="product", referencedColumnName="product")}) public List<Product> products = new ArrayList<Product>(); I can fetch the products for the catalog nicely, but I can't (dynamically) order the products. How could I order them? I probably have to write a many-to-many HQL query with the order-by clause? I though of passing the orderBy field name string to the

hibernate - HQL joins on many clauses

匿名 (未验证) 提交于 2019-12-03 03:06:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I've been reading Hibernate documentation, but I haven't found anything that would explain how to do the following. I have the following SQL code that I'm trying to convert to HQL: SELECT {msg.*}, {cmd.*} FROM Schema.Messages AS msg LEFT OUTER JOIN schema.send_commands AS cmd ON cmd.message_key = msg.unique_key AND ( lower(cmd.status) IN (lower('failed') ) ) WHERE msg.sequence_received < 10"; The mainissue I'm having is that I'm unable to have two clauses on a LEFT OUTER JOIN. HQL allows me to have ON cmd.message_key = msg.unique_key , but

HQL: Hibernate query with ManyToMany

匿名 (未验证) 提交于 2019-12-03 03:05:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a question with HQL query and hibernate. I have a user class and a role class. A user can have many roles. So I have a ManyToMany relatation like this: In user class: @ManyToMany(fetch = FetchType.LAZY) @oinTable(name = "PORTAIL_USERROLE", joinColumns = { @JoinColumn(name = "USERID", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "ROLE", nullable = false, updatable = false) }) public Set<Portailrole> getPortailroles() { return this.portailroles; } In role class: @ManyToMany(fetch = FetchType.LAZY)

Hibernate Query vs Criteria Performance

匿名 (未验证) 提交于 2019-12-03 02:56:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I recently had a rather strange phenomenon. Had to obtain a count that included joins over multiple tables with different WHERE conditions. I implemented the query first with the criteria API of hibernate. It correctly created the requested prepared SQL statement but was rather slow. Re-implemented then the entire query using HQL. Was rather nasty to do that but the result performed much faster than with the Criteria API. Does anybody know the reason for that behavior? I assumed that the Criteria and HQL framework use the same code base to