I want to persist my data into database via hibernate. My model looks like:
campaign | campaign_staffs | staffs
-------------------------------------------------------------
id_campaign (PK) | id_campaign (PK) | id_staff (PK)
.. | id_staff (PK) | ...
.. | ordernumber | ....
cause a campaign can have n staffs and a staff can be responsible for n campaings. So I need a jointable.
The ordernumber of campaign_staffs is the order of the staffs of a campaign. e.g:
id_campaign | id_staff | ordernumber
---------------------------------------------
4711 | 4822 | 1
4711 | 5383 | 2
4711 | 2574 | 3
4812 | 432 | 1
4812 | 9234 | 2
Think you will get it.
So my Entitycode for campaign looks like (snippet):
@Column(name = "id_campaign")
private int id;
@OneToMany(mappedBy ="id.idCampaign")
private Set<CampaignStaff> staffs = new HashSet<CampaignStaff>(0);
Code for staffs:
@Column(name = "id_staffs")
private int id;
@OneToMany(mappedBy ="id.idStaffs")
private Set<CampaignStaff> campaignstaffs = new HashSet<CampaignStaff>(0);
For CampaignStaffs
@EmbeddedId
private CampaignStaffPK id;
private Integer ordernumber;
@ManyToOne
@JoinColumn(name="id_campaign")
private Campaign idCampaign;
@ManyToOne
@JoinColumn(name="id_staffs")
private Staffs idStaffs;
And for CampaignStaffsPK (CompositePK)
@ManyToOne
@JoinColumn(name="id_campaign")
private Campaign idCampaign;
@ManyToOne
@JoinColumn(name="id_staffs")
private Staffs idStaffs;
So if I want so persist something, I get the errormessage:
ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - ERROR: null value in column "ordernumber" violates not-null constraint Detail: Failing row contains (46419, 69829, null).
How do I have to change my code, that hibernate knows, that he has to put a number into order?