2017-07-19 59 views
0

我有這樣的:插入休眠NamedQuery與嵌入式領域

public class A { 

    private B b; 

    @Embedded 
    public B getB() { 
     return b; 
    } 

    public void setB(B b) { 
     this.b = b; 
    } 
} 

@Embeddable 
public class B { 

    private String b1; 
    private String b2; 

    //constructor 
    //accessors 
} 

@NamedQuery(name = "create", query = "insert into A(b1) values ('test')") 

但我得到這個錯誤:

org.hibernate.QueryException: could not resolve property: b1 of: com.blabla.A 

我不知道該怎麼稱呼我的嵌入式類的屬性和我不在互聯網上找到任何例子。是否有人知道我可以如何調用b1屬性?

謝謝。

+0

A類還沒有屬性B1。其屬性稱爲b。 –

+0

是的,但是因爲B被嵌入了,我應該能夠訪問b1對嗎? 我試圖插入A(B.b1)的值...太 –

回答

0

嘗試絲毫這樣的:

public class A { 
@Embedded 
@AttributeOverrides(value={ 
    @AttributeOverride(name = "b1", @Column()), 
    @AttributeOverride(name = "b2", @Column()) 
}) 
private B b; 


public B getB() { 
    return b; 
} 

public void setB(B b) { 
    this.b = b; 
}} 

而且你必須NamedQuery工作:@NamedQuery(name = "create", query = "insert into A(b1) values ('test')")