• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem while using JPA, EJB 3.0

 
shishir choubey
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am very new to EJB 3.0 and JPA and while implementation i have got one issue.
In the application i have 3 table say A,B & C. B has a FK relationship with PK of A. So functionality is when i update a record in A, a new record with some more details has to be entered in B. So while enter the data in B, i have to check a user entered value in C. To achieve this i am creating a PK Object of the C to get the uniqueness.
Everything is working fine but when i am running the test cases A record is updating in A, a new Record is inserting in B & a New record is also inserted in C.
So in C i dont want to insert the record but it is going and inserting a new record.
I guess somewhere i am wrong to use cascadeType, please help me if anybody has came such scenario.
Code -
--------
POJO for Table A
----------------------
@OneToOne(cascade = CascadeType.REMOVE, mappedBy = "aId")
private B bColumn;

POJO for Table B
------------------------
@JoinColumns( {
@JoinColumn(name = "C_ID", referencedColumnName = "C_ID", nullable = false),
@JoinColumn(name = "B_VALUE", referencedColumnName = "VALUE", nullable = false, insertable = false, updatable = false) })
@ManyToOne
private EnumCTable enumCTable;


@JoinColumn(name = "A_ID", referencedColumnName = "A_ID", nullable = false)
@OneToOne(optional = false)
private A aId;


Thanks in Advance,
Shishir



 
shishir choubey
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys i have found the solution for this

Explanation-
If you create a new object, a new object will be inserted. You need to call EntityManager.find() with the id to get the existing object C. You could also call getReference() to just get a reference to the object, which will not access the database.


Thanks,
Shishir
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!