2017-12-23 7 views
0

を選択しますが、問題が発生し、啓発してくださいHibernateは、私は現在、JPA開発プロジェクトを使用しています、新しいオブジェクトのコンストラクタ

例:

@Query(" 
    SELECT 
     new CustomerMaintain(c.content, u, c.createTime, c.updateTime) 
    FROM 
     CustomerMaintain c 
    JOIN 
     c.user u 
    WHERE 
     c.delFlag = FALSE AND c.customer.id = :customerId 
") 

CustomerMaintain.class DTO:私はそこにあると思い

@Entity 
@Table(name = "t_customer_maintain") 
@DynamicUpdate 
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler", "delFlag"}) 
public class CustomerMaintain { 

    public CustomerMaintain() { 
    } 

    public CustomerMaintain(String content, User user, Date createTime, Date updateTime) { 
     this.content = content; 
     this.user = user; 
     this.createTime = createTime; 
     this.updateTime = updateTime; 
    } 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Integer id; 

    @Column(columnDefinition = "VARCHAR(512) DEFAULT ''") 
    private String content; 

    @ManyToOne(optional=false) 
    private User user; 

    @ManyToOne(optional=false) 
    private Customer customer; 

    @Column(columnDefinition = "TINYINT(1) DEFAULT FALSE ",insertable = false,updatable = false) 
    private Boolean delFlag; 

    @Column(columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP",insertable = false,updatable = false) 
    private Date createTime; 

    @Column(columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP",insertable = false,updatable = false) 
    private Date updateTime; 

    // omission getter setter method 

} 

1つのSQLクエリ:

SELECT 
    c.content, 
    u.id, 
    u.name, 
    u.head, 
    c.createTime, 
    c.updateTime 
FROM 
    t_customerMaintain c 
INNER JOIN 
    t_user u ... 

しかし、クエリ結果を休止状態はこれです:

select 
    customerma0_.content as col_0_0_, 
    user1_.id as col_1_0_, /* only user.id fields */ 
    customerma0_.createTime as col_2_0_, 
    customerma0_.updateTime as col_3_0_ 
from 
    t_customer_maintain customerma0_ 
inner join 
    t_user user1_ 
... 

select 
    user0_.id as id1_4_0_, 
    user0_.head as head4_4_0_, 
    user0_.name as name5_4_0_ 
from 
    t_user user0_ 
where 
    user0_.id=? /* user1_.id as col_1_0_ */ 

質問:

なぜ最初のSQLクエリのみuser.id、およびないuser.iduser.nameuser.head

最後のSQLクエリは余計です。

私はこのように書いてみる:

SELECT 
    new CustomerMaintain(c.content, new User(u.id, u.name, u.head), c.createTime, c.updateTime) 

しかし、そうすることが異常に何かをスロー:new User(u.id, u.name, u.head)

私を助けてください、この問題は長い間私を悩ませてきました。

+0

複数の行にコードを書式設定することによって、少なくともあなたに手伝ってください。これは読めるわけではありません。あなたの英語にも関係ない – Alan

+0

あなたの 'CustomerMaintain'クラスを提供してください。 – amir110

+0

@Alan申し訳ありませんが、フォーマットが完了しました。 – guan

答えて

0

NEWオペレータが必要と思われる理由を説明できますか? cのタイプはすでにCustomerMaintainです。したがって、SELECT cで十分です。

Userを明示的に取得する場合は、JOINの代わりにJOIN FETCHを使用してください。この方法では、Userの追加クエリは実行されません。代わりに、Hibernateの@Fetch(FetchMode.JOIN)を使用して、Hibernateに、別のselectクエリの代わりにjoinクエリを使用させ、必要に応じていつでもCustomerMaintain.userをロードすることができます。

クエリの結果にどのプロパティが設定されるかを細かく制御する必要がある場合は、フェッチグラフまたは負荷グラフをクエリヒントとして使用します。 NEWエンティティコンストラクタを使用しないと、このようなクエリから返された結果はではなく、になります。

関連する問題