Jpa cascading removes the inverted relationship
source link: https://www.codesd.com/item/jpa-cascading-removes-the-inverted-relationship.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Jpa cascading removes the inverted relationship
My question is about cascading deletes with JPA and Eclipselink.
I would like to model a simple relationship between two entities: A and B. B references A through a property ref2a (in DB terms B.ref2a is connected to A.id through a foreign key with "ON DELETE CASCADE"). My goal is when an A object is deleted to cascade the delete to all B objects that reference it.
I searched a lot, but I cannot make it work. Most solutions I have found are for the opposite situation: A contains a collection of references to B. This works like a charm. But if the reference is on the B side, I don't know how to do it.
Here is the Code sample:
@Entity
public class A
{
@Id
@GeneratedValue
private Integer id;
private String name;
// ...
}
@Entity
public class B
{
@Id
@GeneratedValue
private Integer id;
private String name;
@OneToOne
@JoinColumn(
foreignKey=@ForeignKey(
foreignKeyDefinition="FOREIGN KEY ref2a REFERENCES A id ON DELETE CASCADE"
)
)
private A ref2a;
// ...
}
And the test code:
public class CascadeTest extends TestCase
{
private EntityManagerFactory emf;
private EntityManager em;
@Override
protected void setUp() throws Exception {
emf = Persistence.createEntityManagerFactory("myDB");
em = emf.createEntityManager();
}
@Override
protected void tearDown() throws Exception {
em.close();
emf.close();
}
public void testApp()
{
Integer aid = -1, bid = -1;
try {
em.getTransaction().begin();
A a = new A();
a.setName("My name is A");
B b = new B();
b.setRef2a(a);
b.setName("My name is B, please delete me when A is gone.");
em.persist(a);
em.persist(b);
em.getTransaction().commit();
aid = a.getId();
bid = b.getId();
} finally {
if (em.getTransaction().isActive())
em.getTransaction().rollback();
}
try {
em.getTransaction().begin();
B b = em.find(B.class, bid);
assertNotNull(b);
assertEquals("My name is B, please delete me when A is gone.", b.getName());
assertEquals("My name is A", b.getRef2a().getName());
assertEquals(aid, b.getRef2a().getId());
A a = em.find(A.class, aid);
assertEquals("My name is A", a.getName());
em.remove(a);
em.getTransaction().commit();
em.getTransaction().begin();
// a should have been removed.
// This passes OK.
a = em.find(A.class, aid);
assertNull(a);
// Cascading deletes should have deleted also b.
b = em.find(B.class, bid);
// PROBLEM: This fails - b is still here.
assertNull(b);
em.getTransaction().commit();
} finally {
if (em.getTransaction().isActive())
em.getTransaction().rollback();
}
}
}
I have solved my problem. Really really simple - my initial code was almost right. I just had a syntax problem in the foreign key cascade. The attributes needed to be in brackets "()", I had overlooked that in the documentation. So the change I needed to do is:
@OneToOne
@JoinColumn(
foreignKey=@ForeignKey(
foreignKeyDefinition="FOREIGN KEY (ref2a) REFERENCES A (id) ON DELETE CASCADE"
)
)
private A ref2a;
Please notice the brackets around the two attributes.
This works, deleting an A object also cascades its linked B objects.
Thanks to everybody for your help!
Recommend
-
76
These are the Best OnePlus phones to buy in 2022 A lot is happening behind the scenes at OnePlus, and it may no longer be the same company that came on the scene back in 2014. B...
-
12
Napkin Problem 13: Filtering with Inverted Indexes This is an edition of the Napkin Math newsletter, a newsletter about using napkin math and first-principle t...
-
6
Napkin Problem 9: Inverted Index Performance and Merkle Tree Syncronization This is an edition of the Napkin Math newsletter, a newsletter about using napkin m...
-
6
Dark mode in 5 minutes, with inverted lightness variables By now, you probably know that you can use custom properties for individual color components, to avoid repeating the same color coordinates m...
-
10
Obtain an inverted slice of a table that includes the base element advertisements I'm using numpy arrays. I'm trying to calculate an array tha...
-
6
Uncertainty prevails as December 2021 Bitcoin futures show an inverted pattern – HodlalertUncertainty prevails as December 2021 Bitcoin futures show an inverted pattern...
-
5
One thing we run into a fair amount in our design system work is the need to clarify the difference between “dark mode” and “components rendered on a dark background”. What is actual dark mode?
-
5
Listview A React Native component that implements SectionList with inverted direction and working sticky header Nov 21, 2021 3 min read In...
-
3
Inverted Triangle Architecture for CSS (ITCSS) Learn abou...
-
4
For context, I’m a bootstrap startup guy, so it doesn’t take much to considered larger. But to help build some guard rails on large, imagine it in the context of people more than money. Think an organization that has 250 + people (but certainly on...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK