映射Hibernate

时间:2014-01-05 01:19:23

标签: java hibernate java-ee

我有2个实体:技术和能力

每个能力可以有1个或更多Techno

所以我已经通过Hibernate生成了类

他给了我3个班级模型

Technologie
Competence
TechnologieId

现在我对如何添加技术感到困惑

TechnoligieId有什么意义?

Technologie.java

public class Technologie implements java.io.Serializable {

    private TechnologieId id;
    private Competence competence;
    private String libelleTechnologie;
}

TechnologieId.java

public class TechnologieId implements java.io.Serializable {

    private int idCompetence;
    private int idTechnologie;
}

Technologie.hbm.xml

 <class name="com.model.Technologie" table="technologie" catalog="gestioncollaborateur">
        <composite-id name="id" class="com.model.TechnologieId">
            <key-property name="idCompetence" type="int">
                <column name="idCompetence" />
            </key-property>
            <key-property name="idTechnologie" type="int">
                <column name="idTechnologie" />
            </key-property>
        </composite-id>
        <many-to-one name="competence" class="com.model.Competence" update="false" insert="false" fetch="select">
            <column name="idCompetence" not-null="true" />
        </many-to-one>
        <property name="libelleTechnologie" type="string">
            <column name="libelleTechnologie" length="254" />
        </property>
    </class>

1 个答案:

答案 0 :(得分:0)

如果在问题中正确提及了这种关系。那你为什么不这样做呢?

public class Technologie implements java.io.Serializable {

private id;
private Competence competence;
private String libelleTechnologie;


@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="<yourForeign Key for Competence>")
public Competence getCompetence() {
    return competence;
}
}

和竞争力等。

public class Competence implements java.io.Serializable {

private id;
private List<Technologie> technologies;


@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(mappedBy = "<name of the property of Competence in Technology class>")
public List<Technologies> getTechnologies() {
    return technologies;
}
}

此外,如果技术是这种关系的管理方面。基本上,这意味着你将在Technologie上进行保存/更新,主要是处理这种关系。

如果您确实希望 @OneToMany方面作为管理方。您必须在getTechnologies方法的@JoinColumn批注中添加insertable = false,updatable = false 属性。