NB: There is a delay for new posters ... as is because the group got spammed - hence this post got delayed a bit, apologies.
The code looks to have a couple of quirks maybe (the way I look at it).
a) I think your observedpacket should be a List of packets? (Perhaps removed in the paste?)
@OneToMany
private ObservedPacket observedpacket;
... should instead be
@OneToMany
private
List<ObservedPacket
> observedpacket;
b) I think you need to add your new packet's to the list of observedpacket (so that the save cascade will cascade and save all the packets in the List.
I believe this is why the cascade is not working. If not perhaps post up the complete example.
Ok, I think there are about 3 questions in here...
Q1
>>
Now one problem is that the track_id is not set in the observed_packet
table - it is always null. How do I tell eBean that there is a
relation?! I played around with cascade but couldn't figure it out.
<<
Some notes about cascade save:
- transaction logs: ... the transaction logs will show you the sql, bind variables and returned generated ids... so firstly I'd recommend finding and looking at the transaction logs (typically in a logs directory ... and when Ebean starts it tells you where the transaction logs are going.
- When you save a 'parent' (in this case track) ... then the cascade will also save the 'details'/packages that are on in the parents Set/List/Map. When Ebean does this it will automatically propagate the ID value from the parent to the children.
... what this means is that the track ID value should automatically be set on the packages that are saved via save cascade.
... depending on the ID generation (DB Identity/Autoincrement vs DB Sequences) this can work a little differently under the covers but your application code should not care.
So, if you can perhaps add a bit of code to add the new packet to the tracks list of packets and give that a go.
Q2
>>
Another problem is that I also get ObservedTracks from the data
source that can be the same = the same ID and these ObservedTracks
shall not be saved. Can I use an annotation so that I don't need to
check first if a ObservedTrack with that id already exists in the
database?
<<
Hmmm, no you can't (there is no such annotation). Typically you could find existing tracks ... something like
Map<?, ObservedTrack> map = Ebean.find(ObservedTrack.class).select("trackID").findMap();
where the key of the map is not defined it defaults to be the @Id property which is the trackIDs. So if the trackId is in this map then it already exists in the DB.
This won't work too well when there is a massive number of track's... as that map will get big...
... an approach (not supported in Ebean currently) that maybe possible with some DBs is to use a "merge" rather than insert/update choice. This could be a good/better approach if there are lots of tracks and if it is supported by your DB and you are writing some batch processing job (processing lots of tracks/packets).
http://en.wikipedia.org/wiki/Merge_%28SQL%29Q3
>>
How do I create relations between ObservedTracks that are
already in the database and Observedpackets that come later on ?
<<
You can find the existing ObservedTrack ...
ObservedTrack track = Ebean.find(ObservedTrack.class, x);
track.addPacket(newPacket);
... assuming you track has a list of ObservedPacket etc.