具有多对一和@IdClass分离实体的Jpa / Hibernate Composite Key

时间:2016-02-15 23:49:46

标签: java hibernate jpa jpa-2.0

我有两个实体培训电台。他们在时间表实体上有@OneToMany关系。在Timetable实体中,我在Train和Station上有复合键作为@ManyToOne。

所有课程都有设置者和获取者

列车

@Entity
@Table(name = "train")
public class Train {

    @Id
    @GeneratedValue
    private Long id;

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

    @OneToMany(mappedBy = "train",fetch = FetchType.EAGER)
    @OrderBy("curentnumber ASC")
    private List<Coach> coaches;

    @OneToMany(mappedBy = "train",fetch = FetchType.LAZY)
    @OrderBy("arrivalTime ASC")
    private List<Timetable> timetable ;
    ...
}

@Entity
@Table(name = "station")
public class Station {

    @Id
    @GeneratedValue
    private Long id;

    @Column(name = "name", length = 16, nullable = false)
    private String name;

    @OneToMany(mappedBy = "station", fetch = FetchType.LAZY)
    private List<Timetable> timetable;

    ...
}

时刻表

@Entity
@IdClass(TimetableId.class)
@Table(name = "timetable")
public class Timetable implements Serializable, Comparable<Timetable>{

    @Id
    @ManyToOne
    @JoinColumn(name = "train_id", referencedColumnName = "id")
    private Train train;

    @Id
    @ManyToOne
    @JoinColumn(name = "station_id", referencedColumnName = "id")
    private Station station;

    @Column(name = "arrivalTime")
    private Timestamp arrivalTime;

    @Column(name = "departureTime")
    private Timestamp departureTime;

    ...
}

TimetableId

public class TimetableId implements Serializable{

    Long train;
    Long station;

    public TimetableId() {
    }
    ...
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        TimetableId that = (TimetableId) o;

        if (train != null ? !train.equals(that.train) : that.train != null) return false;
        return station != null ? station.equals(that.station) : that.station == null;

    }

    @Override
    public int hashCode() {
        int result = train.hashCode();
        result = 31 * result + station.hashCode();
        return result;
    }
}

我也有服务类。

//Result timetable
Timetable addedTimetable = new Timetable();

addedTimetable.setTrain(new Train());
addedTimetable.getTrain().setId(Long.valueOf(trainid));
addedTimetable.setStation(new Station());
addedTimetable.getStation().setId(Long.valueOf(stationid));
try{
    timetableRepository.create(addedTimetable);
} catch (Exception e) {
    e.printStackTrace();
}

还有一些来自GenericDAO

@Override
public void create(T entity) {
    EntityManager manager = emf.createEntityManager();
    try {
        try {
            manager.getTransaction().begin();
            manager.persist(entity);
            manager.getTransaction().commit();
        }
        finally {
            if (manager.getTransaction().isActive())
                manager.getTransaction().rollback();
        }
    } finally {
        manager.close();
    }
}

问题是当我有@IdClass注释并尝试在新的Station()和新的Train上使用现有id引用时创建新时间表,然后我得到错误&#34;传递给persist的分离实体:entity.Station&# 34;在我的dao级别。但是,如果我删除@IdClass注释,那么一切都很好,时间表正确创建。我认为我在TimetableId和注释中犯了错误......

请帮助我,我在这里堆叠。

0 个答案:

没有答案