Hibernate多对多自联接重复条目

时间:2014-08-19 00:31:40

标签: hibernate

我尝试将图表保存到数据库。 它由一个带有“tag”属性的简单类组成。 这堂课可能有父母和孩子。

保存根类后,所有图形也被保存,但当一个节点有两个父节点时,这两个父节点在数据库中重复。

这是映射的节点类:

@Entity
@Table(name="acttest")
public class ActivityTest {

    @Id
    @Column(name="id_activity")
    @GeneratedValue
    private Long idActivity;


    @Column(name="tag")
    private String tag;


    @ManyToMany(cascade=CascadeType.ALL,  fetch = FetchType.EAGER )
    @JoinTable(name="dependencies",
        joinColumns={@JoinColumn(name="id_master")},
        inverseJoinColumns={@JoinColumn(name="id_slave")})
    private Set<ActivityTest> nextActivities = new HashSet<ActivityTest>();


    @ManyToMany(mappedBy="nextActivities",  fetch = FetchType.EAGER)
    private Set<ActivityTest> previousActivities = new HashSet<ActivityTest>();

    (getters and setters)
}

这是我的测试类:

    public static void main(String[] args) {
        Session s;
        ActivityList al = new ActivityList();

        al.addRoot("A");
        al.addNextActivity("A", "A1");
        al.addNextActivity("A", "A2");
        al.addNextActivity("A", "A3");

        al.addNextActivity("A1", "B");
        al.addNextActivity("A1", "BA12");

        al.addNextActivity("A2", "BA12");

        al.addNextActivity("B", "B1");
        al.addNextActivity("B", "B2");

        al.addNextActivity("B2", "B2.2");


        s = ConnFactory.getSession();
        s.beginTransaction();

        s.save( al.getRoot() );

        s.getTransaction().commit();
        s.close();
}

以下是包装列表:

public class ActivityList {
    private List<ActivityTest> activities;
    private ActivityTest root = null;

    public void addRoot( String tag ) {
        if ( root == null ) {
            ActivityTest activity = new ActivityTest( tag );
            activities.add(activity);
            root = activity;
        }
    }

    public ActivityList() {
        activities = new ArrayList<ActivityTest>();
    }

    public void addNextActivity( String master, String next ) {
        ActivityTest newAct = new ActivityTest( next );
        activities.add(newAct);
        for ( ActivityTest act : activities  ) {
            if ( act.getTag().equalsIgnoreCase( master )  ) {
                act.getNextActivities().add( newAct );
            }
        }
    }

    public ActivityTest getRoot() {
        return root;
    }

    public List<ActivityTest> getActivities() {
        return activities;
    }

    public void setActivities( List<ActivityTest> activities ) {
        root = null;
        this.activities = activities;
        for ( ActivityTest act : activities  ) {
            if ( act.getPreviousActivities().size() == 0 ) {
                root = act;
                break;
            }
        }
    }


}

我设计测试的图表:

enter image description here

图表:

enter image description here

依赖表:

enter image description here

注意“BA12”节点的两面性:“A1”孩子的onde和“A2”孩子的其他节点。

如何制作“BA12”节点的一个实例?

1 个答案:

答案 0 :(得分:0)

对不起所有人!

通过更改包装类解决:

添加了私有方法,搜索已创建的对象:

private ActivityTest getActivity( String tag ) {
    for ( ActivityTest act : activities  ) {
        if ( act.getTag().equalsIgnoreCase( tag )  ) {
            return act;
        }
    }
    return new ActivityTest(tag);
}

并将addNextActivity更改为:

public void addNextActivity( String master, String next ) {
    ActivityTest newAct = getActivity( next );
    activities.add(newAct);

    for ( ActivityTest act : activities  ) {
        if ( act.getTag().equalsIgnoreCase( master )  ) {
            act.getNextActivities().add( newAct );
        }
    }
}

再次抱歉。我的错误。