Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JPA-选择、联接、子查询和最大_Java_Sql_Hibernate_Jpa - Fatal编程技术网

Java JPA-选择、联接、子查询和最大

Java JPA-选择、联接、子查询和最大,java,sql,hibernate,jpa,Java,Sql,Hibernate,Jpa,我尝试将这个查询映射到JPA和Join中,子查询和其他查询对我来说非常重要。也许你能帮我。这是我的疑问: SELECT customers.* FROM actions JOIN customers ON customers.id = actions.customer_id WHERE actions.action_type = 'CUSTOMER_TRAINING_BEGIN' AND actions.created IN ( SELECT max(created) FROM actio

我尝试将这个查询映射到JPA和Join中,子查询和其他查询对我来说非常重要。也许你能帮我。这是我的疑问:

SELECT customers.*
FROM actions
JOIN customers
ON customers.id = actions.customer_id
WHERE actions.action_type = 'CUSTOMER_TRAINING_BEGIN' 
AND actions.created IN (
 SELECT max(created)
 FROM actions
 WHERE action_type = 'CUSTOMER_TRAINING_BEGIN' 
 OR action_type = 'CUSTOMER_TRAINING_END'
 GROUP BY customer_id
);
我尝试收集所有客户,其中最后一个状态(客户培训开始或客户培训结束)是客户培训开始。还有其他状态,但我只想要那两个

所以我有这些表格:

                     Table "public.actions"  
   Column    |           Type           |       Modifiers           
-------------+--------------------------+------------------------   
 id          | bigint                   | not null   
 action_type | character varying(30)    | not null   
 user_name   | character varying(30)    | not null   
 customer_id | bigint                   |    
 created     | timestamp with time zone | not null default now()   
 updated     | timestamp with time zone | not null default now()   

                     Table "public.customers"
    Column    |           Type           |       Modifiers        
--------------+--------------------------+------------------------
 id           | integer                  | not null
 fore_name    | character varying(50)    | not null
 last_name    | character varying(50)    | not null
 matrikel     | integer                  | not null
 day_of_birth | timestamp with time zone | not null
 created      | timestamp with time zone | not null default now()
 updated      | timestamp with time zone | not null default now()
以及我的代码片段:

Customer.java

@Data
@Entity
@NoArgsConstructor
@Table(name = "CUSTOMERS")
@EqualsAndHashCode(callSuper = false)
public class Customer extends AbstractTimestampEntity {

    @Id
    @SequenceGenerator(sequenceName = "CUSTOMERS_ID_SEQ", name = "CUSTOMERS_ID_GEN", allocationSize = 1)
    @GeneratedValue(generator = "CUSTOMERS_ID_GEN", strategy = GenerationType.SEQUENCE)
    private long id;

    @Column(name = "FORE_NAME")
    private String foreName;

    @Column(name = "LAST_NAME")
    private String lastName;

    @Column(name = "MATRIKEL")
    private int matrikelNumber;

    @Column(name = "DAY_OF_BIRTH")
    private Date dayOfBirth;

}
ActionType.java

@AllArgsConstructor
public enum ActionType {
    /**
     * Customer starts training.
     */
    CUSTOMER_TRAINING_BEGIN("ActionType.customerTrainingBegin.label"),
    /**
     * Customer stops training
     */
    CUSTOMER_TRAINING_END("ActionType.customerTrainingEnd.label");

    @Getter
    private String labelKey;
}
Action.java

@Data
@Entity
@Table(name = "ACTIONS")
@EqualsAndHashCode(callSuper = false)
public class Action extends AbstractTimestampEntity {

    @Id
    @SequenceGenerator(sequenceName = "ACTIONS_ID_SEQ", name = "actions_id_gen", allocationSize = 1)
    @GeneratedValue(generator = "actions_id_gen", strategy = GenerationType.SEQUENCE)
    private long id;

    @OneToOne
    @JoinColumn(name = "USER_NAME")
    private User user;

    @OneToOne
    @JoinColumn(name = "CUSTOMER_ID")
    private Customer customer;

    @Enumerated(EnumType.STRING)
    @Column(name = "ACTION_TYPE")
    private ActionType type;

}
我的问题是,我必须收集顾客。我必须从行动到客户,因为我没有从客户到行动的参照。所以我不能这样做:

CriteriaBuilder criteriaBuilder = this.em.getCriteriaBuilder();
CriteriaQuery<Customer> query = criteriaBuilder.createQuery(Customer.class);
Root<Customer> from = query.from(Customer.class);

query.select(from);

// Join here to action...
CriteriaBuilder-CriteriaBuilder=this.em.getCriteriaBuilder();
CriteriaQuery=criteriaBuilder.createQuery(Customer.class);
Root-from=query.from(Customer.class);
查询。选择(从中);
//在这里加入行动。。。
也许这有助于理解我的问题。非常感谢


您好,Chesmuh

您还需要映射
操作
表的
已创建
列。然后,您可以应用以下JPQL查询(请注意子选择中的更改;按照您设计SQL查询的方式,它不会带来您想要的结果):


请包括实体/映射的相关部分。如果不知道
Customer
ActionCustomer
之间的关系(例如
ActionCustomer
Customer
?)的子类),这很难回答。对不起,每次都会错过那些该死的小评论…:-/谢谢你的回答。如果我将a.customer与a.customer\u id进行切换,其结果与我的查询相同。但是我试着用JPA和Criteria Builder编写这些查询。请问为什么您更喜欢Criteria而不是JPQL?:)
SELECT a.customer
FROM Action a
WHERE a.type = 'CUSTOMER_TRAINING_BEGIN' 
AND a.created = 
(
SELECT max(created)
FROM Action
WHERE (type = 'CUSTOMER_TRAINING_BEGIN' 
OR type = 'CUSTOMER_TRAINING_END')
AND customer.id = a.customer.id
)