Hibernate OneToMany关系映射澄清

时间:2013-12-04 19:38:17

标签: java hibernate

我为我的表配置了OneToMany映射。因此,当第二次将值插入fisrt表时,它会给我一个错误无法在对象中插入重复的键行。这是我的表格。

表-1(feature_id是pk并由表自动生成),名称由我插入。

   Feature_id(PK) 
   Name 

要素表有索引

"GO
CREATE UNIQUE INDEX idx_Name ON "dbo"."Feature"(Name)

表-2(Feature_Version_id PK和表格自动生成)

   Feature_Version_id ,
   Version_Name 
   Feature_id

POJO课程是:

@Entity
@Table(name="FEATURE")
public class Feature {

    private int feature_id;
    private String name;
    private Set<FeatureVersion> featureversion = new HashSet<FeatureVersion>(0);

    public Feature() {
    }

    public Feature(String name ,Set<FeatureVersion> featureversion) {
        this.name = name;
        this.featureversion = featureversion;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="FEATURE_ID")
    public int getFeature_id() {
        return feature_id;
    }

    public void setFeature_id(int feature_id) {
        this.feature_id = feature_id;
    }

    @Column(name="NAME")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @OneToMany(fetch=FetchType.LAZY,mappedBy="feature")
    public Set<FeatureVersion> getFeatureversion() {
        return featureversion;
    }

    public void setFeatureversion(Set<FeatureVersion> featureversion) {
        this.featureversion = featureversion;
    }
}

第二个表POJO类:

@Entity
@Table(name="FEATURE_VERSION")
public class FeatureVersion {

    private int feature_version_id;
    private String version_name;
    private int feature_id;
    private Feature feature;
    private Set<PartFeatureVersion> partfeatureversion = new HashSet<PartFeatureVersion>(0);

    public FeatureVersion() {
    }


    public FeatureVersion(Feature feature, String version_name, int feature_id) {
        this.feature = feature;
        this.version_name = version_name;
        this.feature_id = feature_id;

    }



    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="FEATURE_VERSION_ID")
    public int getFeature_version_id() {
        return feature_version_id;
    }

    public void setFeature_version_id(int feature_version_id) {
        this.feature_version_id = feature_version_id;
    }

    @Column(name="VERSION_NAME")
    public String getVersion_name() {
        return version_name;
    }

    public void setVersion_name(String version_name) {
        this.version_name = version_name;
    }


    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="feature_id")
    public Feature getFeature() {
        return feature;
    }

    public void setFeature(Feature feature) {
        this.feature = feature;
    }



    @OneToMany(fetch=FetchType.LAZY,mappedBy="featureversion")
    public Set<PartFeatureVersion> getPartfeatureversion() {
        return partfeatureversion;
    }



    public void setPartfeatureversion(Set<PartFeatureVersion> partfeatureversion) {
        this.partfeatureversion = partfeatureversion;
    }
}

主要课程:

Session session = (Session) HibernateUtil.getSessionFactory().openSession();
Feature feature = new Feature();
FeatureVersion featurever = new FeatureVersion();
feature.setName("stack");
session.save(feature);

featurever.setVersion_name("12.2");
featurever.setFeature(feature);
feature.getFeatureversion().add(featurever);
session.save(featurever);
session.getTransaction().commit();
session.flush();

这是我的输入值:

(feature id generated by table)
First attempt for first table: stack
First attempt for second table : 12.2
(feature_id come from feature table)

second attempt for first table: stack
second attempt for second table : 12.3

OneToMany配置会在OneToMany关联中出现多个值时要小心吗?或者我需要检查值是否存在然后只是版本名称?这些所有值都从列表中循环。请告知我该怎么做。

谢谢,

0 个答案:

没有答案