0

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?

Dennis
  • 97
  • 7
  • 1
    Does this answer your question? [Mapping many-to-many association table with extra column(s)](https://stackoverflow.com/questions/5127129/mapping-many-to-many-association-table-with-extra-columns) – XtremeBaumer Dec 17 '19 at 08:30
  • not really. hibernate does not automaticly set the ordernumber with the answer. – Dennis Dec 17 '19 at 10:34
  • Update the question with your new code – XtremeBaumer Dec 17 '19 at 12:25
  • After I updated my code, there was a new Errormessage with int/integer error. So I decided to take my old code and hoping someone can help me with that one. – Dennis Dec 17 '19 at 12:57
  • 1
    I told you they only way to do what you want. Its all in the posted link. You can't do anything with your code and your requirements – XtremeBaumer Dec 17 '19 at 13:10
  • Maybe `@OrderColumn` is what you are looking for. https://docs.jboss.org/hibernate/jpa/2.1/api/javax/persistence/OrderColumn.html – Alan Hay Dec 17 '19 at 13:27

0 Answers0