我在JPQL查詢初學者,我不明白我怎麼可以翻譯JPQL查詢的SQL查詢:JPQL查詢的Java EE項目
我有2個表:
- 客戶(idCustomer,...,idMacroMarket)
- MacroMarket(idMacroMarket,nameMacroMarket ...)
這些表格與關係@ManytoOne(用於客戶)和 @OneToMany(用於MacroMarket)關聯。
SQL查詢:
SELECT nameMacroSegment FROM Macro_market m
INNER JOIN Customer c ON c.idMacroMarket = m.idMacroMarket
WHERE idCustomer = id;
JPQL查詢:
SELECT nameMacroSegment FROM Macro_market m
...
...
WHERE idCustomer = :id
實體
@Entity
@Table(name="macro_market")
public class Macro_market implements Serializable {
private static final long serialVersionUID = 1L;
/** ATTRIBUTES **/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idMacroMarket;
private String nameMacroSegment;
private String nameMarketSegment;
@OneToMany(mappedBy="macMar")
private List<Customer> customers;
...
...
@Entity
@Table(name="customer")
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idCustomer;
...
...
/** RELATIONS **/
// CUSTOMER - MACRO_MARKET
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="idMacroMarket",referencedColumnName="idMacroMarket")
private Macro_market macMar;
感謝您的swers。
這可能工作: SELECT m.nameMacroSegment FROM Macro_market米,m.customersÇ WHERE c.idCustomer =:ID – kostja 2013-03-26 11:42:13
@kostja:確定用於請求,但現在,我有此錯誤:m.customers是不映射[SELECT m.nameMacroSegment FROM com.CustomerRequirement.entities.Macro_market m,m.customers c WHERE c.idCustomer =:id] – 2013-03-26 12:29:56
我剛猜測你的映射可能看起來像什麼。我假設'MakroMarket'與'Customer'具有'OneToMany'關係,因此它可能被稱爲'customers'。您應該使用該字段的實際名稱。 PS - 如果你發佈了兩個實體的代碼 - 只有相關部分 - 類聲明,成員,註釋,沒有方法,沒有評論,這將會有所幫助。 – kostja 2013-03-26 12:34:55