2012-10-31 131 views
4
+-------+ +--------------|  +-------+ 
| BOY | | BOY_GIRL  |  | GIRL | 
+-------+ +--------------|  +-------+ 
| id | | id   |  | id | 
| name | | boy_id  |  | name | 
| birth | | girl_id  |  | birth | 
+-------+ | start_dating |  +-------+ 
      +--------------| 

START_DATING加入的類型是TIMESTAMPDATEHibernate的許多一對多與條件

我有兩個豆男孩和女孩與許多一對多關係

@ManyToMany(fetch = FetchType.LAZY) 
@JoinTable(name = "BOY_GIRL", joinColumns = {@JoinColumn(name = "BOY_ID")}, inverseJoinColumns = {@JoinColumn(name = "GIRL_ID")}) 
public Set<Girl> getGirls() { 
    return girls; 
} 

現在,如何我可以選擇做與查詢HQL,如果我想要得到的名單女孩條件:

where boy_id = (some_boy_id) and START_DATING > (some_timestamp) 
+0

是什麼「的男孩的姑娘名單」? –

+0

我的意思是在SQL它應該看起來像 '選擇girl.name,girl.birth從BOY_GIRL 加盟女孩boy_girl.girl_id = girl.id 其中boy_girl.boy_id =(水木清華)和boy_girl.starting_date>(水木清華)' – beshanoe

+0

你可以顯示女孩課的地圖嗎? –

回答

4

我認爲你必須創建一個BoyGirl類,因爲表BOY_GIRL不是一個簡單的許多一對多表(如果是,則列必須是唯一的boy_idgirl_id)。所以,你應該做的是創造BoyGirl類,然後映射BOYBOY_GIRL一個一對多也映射GIRLBOY_GIRL一個一對多

表關係

+-------+    +--------------+    +-------+ 
| BOY |    | BOY_GIRL  |    | GIRL | 
+-------+    +--------------|    +-------+ 
| id | 0..* --- 1..1 | id   | 1..1 --- 0..* | id | 
| name |    | boy_id  |    | name | 
| birth |    | girl_id  |    | birth | 
+-------+    | start_dating |    +-------+ 
         +--------------+ 

的java班

public class BoyGirl { 
    private long id; 
    private Boy boy; 
    private Girl girl; 
    private Date startDating; 
} 

public class Boy { 
    //other attributes omitted 
    private Set<BoyGirl> boyGirls; 
} 

public class Girl { 
    //other attributes omitted 
    private Set<BoyGirl> boyGirls; 
} 

的選擇曲ERY你需要

// I'm using criteria here, but it will have the same result as your HQL 

public List getGirls(Boy boy, Date startDating) { 
    Criteria c = sessionFactory.getCurrentSession().createCriteria(BoyGirl.class); 
    c.add(Restrictions.eq("boy.id", boy.getId()); 
    c.add(Restrictions.lt("startDating", startDating); 

    List<BoyGirl> boyGirls = (List<BoyGirl>) c.list(); 
    // at this point you have lazily fetch girl attributes 
    // if you need the girl attributes to be initialized uncomment line below 
    // for (BoyGirl boyGirl : boyGirls) Hibernate.initialize(boyGirl.getGirl()); 

    return boyGirls; 
} 
4

我認爲你的實體模型是不正確的,你需要一個代表關係屬性的第三個實體,你應該把男孩和女孩都映射到那個實體。 否則,在您的情況下,無法將關係屬性指定爲starting_date作爲查詢中的條件。 看看this link,你可以找到關於如何映射帶有附加屬性的連接表的詳細解釋。

+0

非常感謝!這正是我所期待的。我還發現了一個很好的例子http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/ – beshanoe

1

由於中間表BOY_GIRL有一些額外的屬性(start_dating),你需要在你的領域模型創建另一箇中間實體爲它,例如:

@Table(name="BOY_GIRL") 
class Relationship { 

    @Id 
    long id; 

    @ManyToOne 
    Boy boy; 

    @ManyToOne; 
    Girl girl; 

    @Column 
    Date startDating; 
} 
0

我不認爲它很好地將約會信息直接保存在連接表中(因爲這樣做的目的應該僅僅是將男孩和女孩關聯起來)。

但是,如果你真的想保持當前的結構,並解決它與HQL,你應該能夠做到像

SELECT g FROM GIRL g, BOY_GIRL bg 
WHERE bg.start_dating = :revelantdate 
    AND bg.boy_id = :boyid 
    AND g.id = bg.girl_id