schauder wrote:
Can you post some code?
Here is what I do in my persistence class. There are two methods: save() and load(), which can be started by the user independently, but not simultaneously.
Code:
private void save()
{
try
{
this.session = this.sessionFactory.getCurrentSession();
Transaction tx = this.session.beginTransaction();
SQLQuery sqlq;
sqlq = this.session.createSQLQuery("truncate abstractnodes, circlelocations, connections, edge_node, edgelocations, edges, environmentevents, event_lane, events, lanes, location_edge, locations, nodes, pointofinterests, polygonlocations, polygonpoints, roadevents, tldurations, trafficlight_edge_tlsl, trafficlights, trafficlightstatelistentries, trafficlightstatelists, way_trafficlight, ways;");
sqlq.executeUpdate();
//[...] Then I set the IDs of all objects to 0.
for(Edge e: this.edges)
{
for(Lane l: e.getLanes())
{
session.save(l);
}
session.save(e);
}
for(Way w: this.ways)
{
session.save(w);
}
for(Node n: this.nodes)
{
if(n instanceof TrafficLight)
{
TrafficLight tl = (TrafficLight)n;
for(Entry<Edge, TrafficLightStateList> entry: tl.getDefaultLogic().entrySet())
{
for(TrafficLightStateListEntry states: entry.getValue().getStates())
{
this.session.save(states);
}
this.session.save(entry.getValue()); }
}
session.save(n);
}
for(PointOfInterest p: this.pois)
{
session.save(p);
}
for(Event ev: this.events)
{
session.save(ev);
session.save(ev.getLocation());
}
tx.commit();
catch(Exception e)
{
e.printStackTrace();
}
Code:
private void load()
{
this.session = this.sessionFactory.getCurrentSession();
Transaction tx = this.session.beginTransaction();
Query q;
q = this.session.createQuery("from PointOfInterest");
List<PointOfInterest> pois = q.list();
q = this.session.createQuery("from Edge");
List<Edge> edges = q.list();
q = this.session.createQuery("from Node");
List<Node> nodes = q.list();
q = this.session.createQuery("from Way");
List<Way> ways = q.list();
this.setEdges(new ArrayList<Edge>(edges));
this.setNodes(new ArrayList<Node>(nodes));
this.setWays(new ArrayList<Way>(ways));
this.setPointOfInterests(pois);
Query q;
q = this.session.createQuery("from EnvironmentEvent");
List<EnvironmentEvent> environmentevents = q.list();
q = this.session.createQuery("from RoadEvent");
List<RoadEvent> roadevents = q.list();
this.setRoadEvents(new ArrayList<RoadEvent>(roadevents));
this.setEnvironmentEvents(new ArrayList<EnvironmentEvent>(environmentevents));
tx.commit();
catch(Exception e)
{
e.printStackTrace();
}
The data classes that cause problems here are:
Code:
@Entity
@Table(name="events")
@Inheritance(strategy=InheritanceType.JOINED)
@SequenceGenerator(name="SEQ_EVENT", sequenceName="events_id_seq")
public abstract class Event extends DeepCloner implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_EVENT")
public int id;
private int startTime;
private int endTime;
@OneToOne(fetch=FetchType.EAGER)
@JoinColumn(name="location")
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE, CascadeType.ALL})
protected Location location;
[...]
Code:
@Entity
@Table(name="environmentevents")
public class EnvironmentEvent extends Event implements Serializable {
//[...] no additional relationships
Code:
@Entity
@Table(name="roadevents")
public class RoadEvent extends Event implements Serializable {
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(
name="event_lane",
joinColumns={@JoinColumn(name="event")},
inverseJoinColumns={@JoinColumn(name="lane")}
)
@IndexColumn(name = "indexinroadevent", base=0)
@Cascade({CascadeType.SAVE_UPDATE, CascadeType.PERSIST})
private List<Lane> blockedLanes = new ArrayList<Lane>();
[...]
Code:
@Entity
@Table(name="locations")
@Inheritance(strategy=InheritanceType.JOINED)
@SequenceGenerator(name="SEQ_LOC", sequenceName="locations_id_seq")
public abstract class Location extends DeepCloner implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_LOC")
public int id;
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(
name="location_edge",
joinColumns={@JoinColumn(name="location")},
inverseJoinColumns={@JoinColumn(name="edge")}
)
@Cascade({CascadeType.SAVE_UPDATE, CascadeType.PERSIST})
protected List<Edge> edges;
[...]
Code:
@Entity
@Table(name="edgelocations")
public class EdgeLocation extends Location implements Serializable {
[...]
Code:
@Entity
@Table(name="circlelocations")
public class CircleLocation extends Location implements Serializable {
@Embedded
@AttributeOverrides( {
@AttributeOverride(name="longitude", column = @Column(name="long") ),
@AttributeOverride(name="latitude", column = @Column(name="lat") ),
@AttributeOverride(name="altitude", column = @Column(name="alt"))
} )
private GlobalPosition center;
Code:
@Entity
@Table(name="polygonlocations")
public class PolygonLocation extends Location {
/**
*
*/
private static final long serialVersionUID = -6319440118425086537L;
@CollectionOfElements(fetch=FetchType.EAGER)
@JoinTable( name="polygonpoints",
joinColumns = @JoinColumn(name="polygonlocation") )
@IndexColumn(name = "indexinlocation", base=0)
private List<GlobalPosition> points = new ArrayList<GlobalPosition>();
Maybe this helps?