質問

私はかなり長い間それに取り組んできましたが、それでも私のコードの何が悪いのかを理解することはできません。各サービスには複数のプロファイルがありますが、各プロファイルには1つのサービスしかありません。

Service
{
Long service_id; // primary key
... getter/setter
}

Profile
{
Long profile_id; // primary key
Long service_id; // foreign key
... getter and setter
}

inprofile.hbm.xml。私は追加します

< many-to-one name="service_id" class="com.mot.diva.dto.Service" column="SERVICE_ID" cascade="save-update">
< /many-to-one>

それをマッピングする正しい方法ですか?

役に立ちましたか?

解決

各サービスには複数のプロファイルがありますが、各プロファイルには1つのサービスしかありません。

次に、それに応じてオブジェクトモデルを設計します。 ORMツールを使用する場合、IDSではなくオブジェクト(エンティティ)とエンティティ間の関係を考える必要があります。 ORMはPK、FK、参加などの世話をします。そのため、コードは次のようなものでなければなりません。

public class Service implements Serializable {
    private Long service_id; // primary key
    private Set<Profile> profiles = new HashSet<Profile>();

    // ... getter/setter
}

public class Profile implements Serializable {
    private Long profile_id; // primary key
    private Service service;

    // ... getter and setter
}

そしてその Profile.hbm.xml マッピングファイル:

....
<many-to-one name="service" 
             class="com.mot.diva.dto.Service"
             column="SERVICE_ID"
             cascade="save-update">
</many-to-one>
...

とで Service.hbm.xml (あなたの協会は双方向であるように見えるため):

...
<set name="profiles" inverse="true">
    <key column="PROFILE_ID" not-null="true"/>
    <one-to-many class="com.mot.diva.dto.Profile"/>
</set>
...
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top