how to map a id which comes from a different table
Hello,I have a situation where I have a table that references another table:
(postgresql)
create table event ( eventId serial primary key, eventType text
);
create table enforcement ( enforcementId integer references event primary key, name text
);
What I would like to happen is when I save a new enforcement hibernate will create an event. Then the enforcement would reference the new event. Any suggestions on how I could map this? What I want essentially is to have the id for enforcement generated from the new event. What I'm thinking I can do is have:
lt;generator class=quot;assignedquot;gt;
but I don't see very much documentaion on this and was wondering if someone could shed some light on it. I'm open to other suggestions if that isn't the correct way of thinking of this problem.
thanks,
Mike
I should have explained this earlier. The other problem I face is I need the eventType to correspond to the table that references it. There are several other tables that reference the Event table for their primary key. Essentially, the event table is a sequence used by other tables.
So if I am inserting an Enforcement, then the Event table needs something like 'enforcement' for the eventType value.
Currently, we have the following working:Code:
public void createNewEnforcement(Enforcement e)
{ Event ev = new Event(); ev.setEventType(quot;enforcementquot;); session.save(ev); e.setId( ev.getId() ); session.save(e);
}
This works, but I feel that there might be a better way of doing it.
Page:
[1]