How to properly use mapping in Entity to prevent Oracle error on persist?

时间:2018-12-19 11:11:27

标签: java oracle hibernate

I'm not a genius at connecting Hibernate Entities in Java, so this could be a very trivial question, but I'm having really hard time now and can't figure out how to solve this...

I'm having a GENERATION table and a connected LIMIT_LIST. The LIMIT_LIST has element, which are stored in a 3rd table (not really relevant). The LIMIT_LIST has a 3column primary key and these three column are represented also in the GENERATION table and linked together to get a relationship.

One Generation element can have one element in the Limit_list table.

This is the relevant part in my Generation Entity:

@ManyToOne(fetch=FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumns( {
    @JoinColumn(name="LIMIT_LIST_OBJID", referencedColumnName="OBJID", nullable=false, insertable=false, updatable=false),
    @JoinColumn(name="LIMIT_LIST_TY", referencedColumnName="LIMIT_LIST_TY", nullable=false, insertable=false, updatable=false),
    @JoinColumn(name="LIMIT_LIST_KIND_CV", referencedColumnName="LIMIT_LIST_KIND_CV", nullable=false, insertable=false, updatable=false) } )
private LimitList limitList;

The relevant Foreign key in Oracle Table:

Foreign Key
COLUMN_NAME         COLUMN_POSITION
LIMIT_LIST_OBJID    1
LIMIT_LIST_TY       2
LIMIT_LIST_KIND_CV  3

links to: LIMIT_LIST table

The relevant part in LimitList Entity:

@OneToMany(mappedBy = "limitList", fetch = FetchType.EAGER)
private Set<Generation> generations = new HashSet<Generation>();

This has as Primary Key the three columns:

Primary Key
COLUMN_NAME         COLIUMN_POSITION
OBJID               1
LIMIT_LIST_TY       2
LIMIT_LIST_KIND_CV  3

My problem: if I make this calls in a row in my code:

EntityManager.persist(limitList);
EntityManager.persist(limitListElements);
EntityManager.persist(generation);

I get ORA-00001 exception. It seems to me, that Hibernate tries to insert the limitList twice... That's why Oracle gives the Primary Key error.

If I remove the limitList and limitListElements persist part, because I assume Hibernate will take care of ...

EntityManager.persist(generation);

it I got this Oracle Exception:

ORA-02291

Nothing seems to work and I don't know what did I wrong in the Entity configs... Can you please help me out? Thank you :)

PS.: this two persist functions are working fine:

EntityManager.persist(limitList);
EntityManager.persist(limitListElements);

The list and the elements are stored without any errors, the problem comes when I try to connect the list with my Generation...

EDIT:

the limitList Entity and they relations

@OneToMany(mappedBy = "limitList", fetch = FetchType.LAZY)
private Set<LimitListElement> elements = new HashSet<RestrictionListElement>();

@OneToMany(mappedBy = "limitList", fetch = FetchType.EAGER)
private Set<Generation> generations = new HashSetGeneration>();

EDIT2:

This is the compositeId of the limitList:

@EmbeddedId
@AttributeOverrides( {
    @AttributeOverride(name="objId", column=@Column(name="OBJID", nullable=false, precision=22, scale=0) ),
    @AttributeOverride(name="limitListTy", column=@Column(name="LIMIT_LIST_TY", nullable=false, precision=22, scale=0) ),
    @AttributeOverride(name="limitListKindCv", column=@Column(name="LIMIT_LIST_KIND_CV", nullable=false, precision=22, scale=0) ) } )
private LimitListId id;

1 个答案:

答案 0 :(得分:0)

The EntityManager.persist(generation); should be enough to persist also the LimitList.

You have to make sure that the LimitList.generations is filled in with the generation object you pass into persist:

Generation generation = // create Generation entity
LimitList limitList = // create LimitList entity

limitList.getGenerations().add(generation);
generation.setLimitList(limiList);

EntityManager.persist(generation);

Not sure about limitListElements as there is no entity description..