[JIRA] (HHH-17033) Invalid SQL generated when implicit joins are used
by Bogdan Ilchyshyn (JIRA)
Bogdan Ilchyshyn ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... ) *updated* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiNDEzNzRjNjU4... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-17033?atlOrigin=eyJpIjoiNDEzNz... ) HHH-17033 ( https://hibernate.atlassian.net/browse/HHH-17033?atlOrigin=eyJpIjoiNDEzNz... ) Invalid SQL generated when implicit joins are used ( https://hibernate.atlassian.net/browse/HHH-17033?atlOrigin=eyJpIjoiNDEzNz... )
Change By: Bogdan Ilchyshyn ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... )
Consider the following query which uses implicit joins:
{noformat}select r.id, flr.id, ur1.id, ur2.id, ur3.id from RootEntity r
inner join r.firstLevelReference as flr
left join UnrelatedEntity ur1 on ur1.id = flr.secondLevelReferenceA.id
left join UnrelatedEntity ur2 on ur2.id = flr.secondLevelReferenceB.id
left join UnrelatedEntity ur3 on ur3.id = flr.secondLevelReferenceB.thirdLevelReference.id {noformat}
Generated SQL is invalid as it references the alias in join conditions before it was declared:
{noformat}select
r1_0.id,
r1_0.firstLevelReference_id,
u1_0.id,
u2_0.id,
u3_0.id
from
RootEntity r1_0
join
FirstLevelReferencedEntity f1_0
on f1_0.id=r1_0.firstLevelReference_id
left join
UnrelatedEntity u1_0
on u1_0.id=f1_0.secondLevelReferenceA_id
left join
UnrelatedEntity u2_0
on u2_0.id=f1_0.secondLevelReferenceB_id
left join
UnrelatedEntity u3_0
on u3_0.id=s2_0.thirdLevelReference_id
join
SecondLevelReferencedEntityB s2_0
on s2_0.id=f1_0.secondLevelReferenceB_id{noformat}
and produces And exception happens :
{noformat}2023-08-04 13:32:02 WARN SqlExceptionHelper:145 - SQL Error: 42122, SQLState: 42S22
2023-08-04 13:32:02 ERROR SqlExceptionHelper:150 - Column "S2_0.THIRDLEVELREFERENCE_ID" not found; SQL statement:{noformat}
This query, however, works on Hibernate 5.x - the generated SQL is valid on this version.
Please see a reproducer attached :
[^hibernate-path-sql-issue-. zip]
( https://hibernate.atlassian.net/browse/HHH-17033#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-17033#add-comment?atlOrigin=ey... )
Get Jira notifications on your phone! Download the Jira Cloud app for Android ( https://play.google.com/store/apps/details?id=com.atlassian.android.jira.... ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailN... ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100233- sha1:06f5c37 )
11 months
[JIRA] (HHH-17033) Invalid SQL generated when implicit joins are used
by Bogdan Ilchyshyn (JIRA)
Bogdan Ilchyshyn ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... ) *created* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiY2VmOGE4YmU4... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-17033?atlOrigin=eyJpIjoiY2VmOG... ) HHH-17033 ( https://hibernate.atlassian.net/browse/HHH-17033?atlOrigin=eyJpIjoiY2VmOG... ) Invalid SQL generated when implicit joins are used ( https://hibernate.atlassian.net/browse/HHH-17033?atlOrigin=eyJpIjoiY2VmOG... )
Issue Type: Bug Affects Versions: 6.2.7 Assignee: Unassigned Attachments: hibernate-path-sql-issue-.zip Created: 03/Aug/2023 20:40 PM Priority: Major Reporter: Bogdan Ilchyshyn ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... )
Consider the following query which uses implicit joins:
select r.id, flr.id, ur1.id, ur2.id, ur3.id from RootEntity r
inner join r.firstLevelReference as flr
left join UnrelatedEntity ur1 on ur1.id = flr.secondLevelReferenceA.id
left join UnrelatedEntity ur2 on ur2.id = flr.secondLevelReferenceB.id
left join UnrelatedEntity ur3 on ur3.id = flr.secondLevelReferenceB.thirdLevelReference.id
Generated SQL is invalid as it references the alias in join conditions before it was declared:
select
r1_0.id,
r1_0.firstLevelReference_id,
u1_0.id,
u2_0.id,
u3_0.id
from
RootEntity r1_0
join
FirstLevelReferencedEntity f1_0
on f1_0.id=r1_0.firstLevelReference_id
left join
UnrelatedEntity u1_0
on u1_0.id=f1_0.secondLevelReferenceA_id
left join
UnrelatedEntity u2_0
on u2_0.id=f1_0.secondLevelReferenceB_id
left join
UnrelatedEntity u3_0
on u3_0.id=s2_0.thirdLevelReference_id
join
SecondLevelReferencedEntityB s2_0
on s2_0.id=f1_0.secondLevelReferenceB_id
and produces exception:
2023-08-04 13:32:02 WARN SqlExceptionHelper:145 - SQL Error: 42122, SQLState: 42S22
2023-08-04 13:32:02 ERROR SqlExceptionHelper:150 - Column "S2_0.THIRDLEVELREFERENCE_ID" not found; SQL statement:
This query, however, works on Hibernate 5.x - the generated SQL is valid.
Please see a reproducer attached.
( https://hibernate.atlassian.net/browse/HHH-17033#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-17033#add-comment?atlOrigin=ey... )
Get Jira notifications on your phone! Download the Jira Cloud app for Android ( https://play.google.com/store/apps/details?id=com.atlassian.android.jira.... ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailN... ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100233- sha1:06f5c37 )
11 months
[JIRA] (HHH-17032) H2 Database: PostgreSQL Dialect Mode
by Rick O'Sullivan (JIRA)
Rick O'Sullivan ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=712020%... ) *created* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiZjY2MGFmMDll... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-17032?atlOrigin=eyJpIjoiZjY2MG... ) HHH-17032 ( https://hibernate.atlassian.net/browse/HHH-17032?atlOrigin=eyJpIjoiZjY2MG... ) H2 Database: PostgreSQL Dialect Mode ( https://hibernate.atlassian.net/browse/HHH-17032?atlOrigin=eyJpIjoiZjY2MG... )
Issue Type: Bug Affects Versions: 6.2.7 Assignee: Unassigned Components: hibernate-core Created: 03/Aug/2023 15:05 PM Environment: org.hibernate.orm:hibernate-core:jar:6.2.7.Final
com.h2database:h2:jar:2.2.220
openjdk version "17.0.8" 2023-07-18
Ubuntu 22.04.2 LTS Priority: Minor Reporter: Rick O'Sullivan ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=712020%... )
I use the H2 database in PostgreSQL mode, in development.
# H2 PostgreSQL Mode: h2db database in local file.
jakarta.persistence.jdbc.driver=org.h2.Driver
jakarta.persistence.jdbc.user=tester
jakarta.persistence.jdbc.password=123456
jakarta.persistence.jdbc.url=jdbc:h2:file:./target/test-database/h2db;MODE=PostgreSQL
hibernate.dialect=org.hibernate.dialect.PostgreSQL10Dialect
I just updated my Hibernate dependencies to 6.x from 5.x. Specifically, to 6.2.7.Final.
When I use PostgreSQL10Dialect, I get this warning:
WARN deprecation - HHH90000026: PostgreSQL10Dialect has been deprecated; use org.hibernate.dialect.PostgreSQLDialect instead
But when I use PostgreSQLDialect, I get this warning:
WARN Dialect - HHH000511: The 2.2.0 version for [org.hibernate.dialect.PostgreSQLDialect] is no longer supported, hence certain features may not work properly. The minimum supported version is 10.0.0. Check the community dialects project for available legacy versions.
What is the plan, going forward, to select a Hibernate dialect for H2 in PostgreSQL mode?
I would like to use a proper dialect setting that does not produce a warning.
>
>
>
> Note: H2 ( http://www.h2database.com/html/features.html#compatibility ) supports
> several SQL modes: IBM DB2, Apache Derby, HSQLDB, MS SQL Server, MySQL,
> Oracle, and PostgreSQL.
>
>
I am concerned that the deprecated PostgreSQL10Dialect will be removed before H2 SQL Modes is supported by Hibernate dialects.
( https://hibernate.atlassian.net/browse/HHH-17032#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-17032#add-comment?atlOrigin=ey... )
Get Jira notifications on your phone! Download the Jira Cloud app for Android ( https://play.google.com/store/apps/details?id=com.atlassian.android.jira.... ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailN... ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100233- sha1:3ea1a2a )
11 months
[JIRA] (HHH-17009) Hibernate 6.2 migration guide does explain how to deal with removal of AbstractPostInsertGenerator (affects IdentityGenerator)
by Gavin King (JIRA)
Gavin King ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... ) *commented* on HHH-17009 ( https://hibernate.atlassian.net/browse/HHH-17009?atlOrigin=eyJpIjoiN2QwMD... )
Re: Hibernate 6.2 migration guide does explain how to deal with removal of AbstractPostInsertGenerator (affects IdentityGenerator) ( https://hibernate.atlassian.net/browse/HHH-17009?atlOrigin=eyJpIjoiN2QwMD... )
I agree that the migration guide should mention that{{AbstractPostInsertGenerator}} was removed. In fact, I’m not even clear why I decided to actually remove this class rather than leaving:
@Deprecated
public abstract class AbstractPostInsertGenerator
implements PostInsertIdentifierGenerator, BulkInsertionCapableIdentifierGenerator {}
In fact, I’m not against putting a stub like this back , for the purpose of backward compatibility.
That said, I’m not sure I understand any of the other objections in this issue report, nor do I understand where the alleged “bug” might be located.
* A PostInsertIdentifierGenerator should never be doing things in generate() , since that method is called before the insert. The contract prior to 6.3 used to be that a PostInsertIdentifierGenerator was supposed to just return the dummy value POST_INSERT_INDICATOR from that method. Now, with the new, much improved design, a PostInsertIdentifierGenerator doesn’t have that method at all. So the displayed code example simply doesn’t seem to make much sense to my eyes.
* It’s simply not correct that IdentityGenerator is missing the methods it used to inherit from AbstractPostInsertGenerator. All that has happened is that those methods have become default methods of the super-interfaces, completely obviating the need for AbstractPostInsertGenerator.
So the migration path from AbstractPostInsertGenerator is simply to inherit BulkInsertionCapableIdentifierGenerator and PostInsertIdentifierGenerator directly.
We could either method that in the migration guide, or put back the stub implementation of AbstractPostInsertGenerator that I show above. (I don’t care which.)
( https://hibernate.atlassian.net/browse/HHH-17009#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-17009#add-comment?atlOrigin=ey... )
Get Jira notifications on your phone! Download the Jira Cloud app for Android ( https://play.google.com/store/apps/details?id=com.atlassian.android.jira.... ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailN... ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100233- sha1:3ea1a2a )
11 months
[JIRA] (HHH-17031) HQL - DELETE does not delete child entity.
by Satish (JIRA)
Satish ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=5f990cf... ) *updated* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiN2M4NGRmYTdi... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-17031?atlOrigin=eyJpIjoiN2M4NG... ) HHH-17031 ( https://hibernate.atlassian.net/browse/HHH-17031?atlOrigin=eyJpIjoiN2M4NG... ) HQL - DELETE does not delete child entity. ( https://hibernate.atlassian.net/browse/HHH-17031?atlOrigin=eyJpIjoiN2M4NG... )
Change By: Satish ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=5f990cf... )
{noformat}When using HQL to DELETE a parent entity,
the delete is not cascaded to child entity.
Here is a snippet of code that has the issues, detailed test case is below.
transaction.begin();
// Use HQL to delete the parent. With CASCADE.ALL,
// the child should be deleted. --- DOES NOT DELETE
Query query = em.createQuery("DELETE FROM ParentEntity WHERE id = :parentId");
query.setParameter("parentId", parentId);
query.executeUpdate();
transaction.commit();
But this piece of code cascades and deletes the child entity.
// Delete using EM ---- WORKS FINE!
int parentId = parentEntity1.getId();
transaction.begin();
ParentEntity retrievedParent = em.find(ParentEntity.class, parentId);
em.remove(retrievedParent);
transaction.commit();
-- Database tables needed for the test case
create table TEST.TEST_PARENT_TABLE
(
ID INTEGER not null primary key,
NAME VARCHAR(256)
);
create table TEST.TEST_CHILD_TABLE
(
ID INTEGER not null primary key,
NAME VARCHAR(256)
);{noformat}
----ParentEntity.java --------
{noformat}package com.example;
import jakarta.persistence.*;
import java.io.Serializable;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
@Entity
@Table(name = "TEST_PARENT_TABLE", schema = "TEST")
public class ParentEntity implements Serializable {
private Integer id;
private String name;
private ChildEntity childEntity;
@Id
@Column(name = "ID")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "NAME")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
@PrimaryKeyJoinColumn(name = "ID")
@NotFound(action = NotFoundAction.IGNORE)
public ChildEntity getChildEntity() {
return childEntity;
}
public void setChildEntity(ChildEntity childEntity) {
this.childEntity = childEntity;
}{noformat}
--ChildEntity.java
{noformat}package com.example;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import java.io.Serializable;
@Entity
@Table(name = "TEST_CHILD_TABLE", schema = "TEST")
public class ChildEntity implements Serializable {
private Integer id;
private String name;
@Id
@Column(name = "ID")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "NAME")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
} {noformat}
--JUnit Test
{noformat} package com.example;
import static org.junit.Assert.assertNull;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.EntityTransaction;
import jakarta.persistence.Query;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class TestParentChildRelationship {
private EntityManagerFactory emf;
private EntityManager em;
@Before
public void setup() {
emf = JpaConfig.getEntityManagerFactory();
em = emf.createEntityManager();
}
@After
public void tearDown() {
if (em != null) {
em.close();
}
if (emf != null) {
emf.close();
}
}
@Test
public void testParentChildRelationship() {
EntityTransaction transaction = em.getTransaction();
transaction.begin();
// Create Parent entity 1
ParentEntity parentEntity1 = new ParentEntity();
parentEntity1.setId(1);
parentEntity1.setName("parent entity 1");
ChildEntity childEntity1 = new ChildEntity();
childEntity1.setId(1);
childEntity1.setName("Child of parent entity 1");
parentEntity1.setChildEntity(childEntity1);
em.persist(parentEntity1);
// Create Parent entity 2
ParentEntity parentEntity2 = new ParentEntity();
parentEntity2.setId(2);
parentEntity2.setName("parent entity 2");
ChildEntity childEntity2 = new ChildEntity();
childEntity2.setId(2);
childEntity2.setName("Child of parent entity 2");
parentEntity2.setChildEntity(childEntity2);
em.persist(parentEntity2);
transaction.commit();
// Delete using EM
int parentId = parentEntity1.getId();
transaction.begin();
ParentEntity retrievedParent = em.find(ParentEntity.class, parentId);
em.remove(retrievedParent);
transaction.commit();
// Delete using HQL
parentId = parentEntity2.getId();
transaction = em.getTransaction();
transaction.begin();
// Use HQL to delete the parent .... with cascade all, the child should be deleted.
Query query = em.createQuery("DELETE FROM ParentEntity WHERE id = :parentId");
query.setParameter("parentId", parentId);
query.executeUpdate();
transaction.commit();
assertNull(
"Parent entity 1 should be deleted", em.find(ParentEntity.class, parentEntity1.getId()));
assertNull(
"Parent entity 2 should be deleted", em.find(ParentEntity.class, parentEntity2.getId()));
assertNull(
"Child entity 1 should be deleted along with the parent",
em.find(ChildEntity.class, childEntity1.getId()));
assertNull(
"Child entity 2 should be deleted along with the parent",
em.find(ChildEntity.class, childEntity2.getId()));
}
}{noformat}
( https://hibernate.atlassian.net/browse/HHH-17031#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-17031#add-comment?atlOrigin=ey... )
Get Jira notifications on your phone! Download the Jira Cloud app for Android ( https://play.google.com/store/apps/details?id=com.atlassian.android.jira.... ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailN... ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100233- sha1:3ea1a2a )
11 months
[JIRA] (HHH-17031) HQL - DELETE does not delete child entity.
by Satish (JIRA)
Satish ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=5f990cf... ) *updated* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiNjA4MjhhYzYz... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-17031?atlOrigin=eyJpIjoiNjA4Mj... ) HHH-17031 ( https://hibernate.atlassian.net/browse/HHH-17031?atlOrigin=eyJpIjoiNjA4Mj... ) HQL - DELETE does not delete child entity. ( https://hibernate.atlassian.net/browse/HHH-17031?atlOrigin=eyJpIjoiNjA4Mj... )
Change By: Satish ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=5f990cf... )
{noformat}When using HQL to DELETE a parent entity,
the delete is not cascaded to child entity.
Here is a snippet of code that has the issues, detailed test case is below.
transaction.begin();
// Use HQL to delete the parent. With CASCADE.ALL,
// the child should be deleted. --- DOES NOT DELETE
Query query = em.createQuery("DELETE FROM ParentEntity WHERE id = :parentId");
query.setParameter("parentId", parentId);
query.executeUpdate();
transaction.commit();
But this piece of code cascades and deletes the child entity.
// Delete using EM ---- WORKS FINE!
int parentId = parentEntity1.getId();
transaction.begin();
ParentEntity retrievedParent = em.find(ParentEntity.class, parentId);
em.remove(retrievedParent);
transaction.commit();
-- Database tables needed for the test case
create table TEST.TEST_PARENT_TABLE
(
ID INTEGER not null primary key,
NAME VARCHAR(256)
);
create table TEST.TEST_CHILD_TABLE
(
ID INTEGER not null primary key,
NAME VARCHAR(256)
);{noformat}
----ParentEntity.java --------
{noformat}package com.example;
import jakarta.persistence.*;
import java.io.Serializable;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
@Entity
@Table(name = "TEST_PARENT_TABLE", schema = "TEST")
public class ParentEntity implements Serializable {
private Integer id;
private String name;
private ChildEntity childEntity;
@Id
@Column(name = "ID")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "NAME")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
@PrimaryKeyJoinColumn(name = "ID")
@NotFound(action = NotFoundAction.IGNORE)
public ChildEntity getChildEntity() {
return childEntity;
}
public void setChildEntity(ChildEntity childEntity) {
this.childEntity = childEntity;
}{noformat}
--ChildEntity.java
{noformat}package com.example;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import java.io.Serializable;
@Entity
@Table(name = "TEST_CHILD_TABLE", schema = "TEST")
public class ChildEntity implements Serializable {
private Integer id;
private String name;
@Id
@Column(name = "ID")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "NAME")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
{noformat}
( https://hibernate.atlassian.net/browse/HHH-17031#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-17031#add-comment?atlOrigin=ey... )
Get Jira notifications on your phone! Download the Jira Cloud app for Android ( https://play.google.com/store/apps/details?id=com.atlassian.android.jira.... ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailN... ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100233- sha1:3ea1a2a )
11 months
[JIRA] (HHH-17031) HQL - DELETE does not delete child entity.
by Satish (JIRA)
Satish ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=5f990cf... ) *commented* on HHH-17031 ( https://hibernate.atlassian.net/browse/HHH-17031?atlOrigin=eyJpIjoiOWFlOD... )
Re: HQL - DELETE does not delete child entity. ( https://hibernate.atlassian.net/browse/HHH-17031?atlOrigin=eyJpIjoiOWFlOD... )
package com.example;
import static org.junit.Assert.assertNull;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.EntityTransaction;
import jakarta.persistence.Query;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class TestParentChildRelationship {
private EntityManagerFactory emf;
private EntityManager em;
@Before
public void setup() {
emf = JpaConfig.getEntityManagerFactory();
em = emf.createEntityManager();
}
@After
public void tearDown() {
if (em != null) {
em.close();
}
if (emf != null) {
emf.close();
}
}
@Test
public void testParentChildRelationship() {
EntityTransaction transaction = em.getTransaction();
transaction.begin();
// Create Parent entity 1
ParentEntity parentEntity1 = new ParentEntity();
parentEntity1.setId(1);
parentEntity1.setName("parent entity 1");
ChildEntity childEntity1 = new ChildEntity();
childEntity1.setId(1);
childEntity1.setName("Child of parent entity 1");
parentEntity1.setChildEntity(childEntity1);
em.persist(parentEntity1);
// Create Parent entity 2
ParentEntity parentEntity2 = new ParentEntity();
parentEntity2.setId(2);
parentEntity2.setName("parent entity 2");
ChildEntity childEntity2 = new ChildEntity();
childEntity2.setId(2);
childEntity2.setName("Child of parent entity 2");
parentEntity2.setChildEntity(childEntity2);
em.persist(parentEntity2);
transaction.commit();
// Delete using EM
int parentId = parentEntity1.getId();
transaction.begin();
ParentEntity retrievedParent = em.find(ParentEntity.class, parentId);
em.remove(retrievedParent);
transaction.commit();
// Delete using HQL
parentId = parentEntity2.getId();
transaction = em.getTransaction();
transaction.begin();
// Use HQL to delete the parent .... with cascade all, the child should be deleted.
Query query = em.createQuery("DELETE FROM ParentEntity WHERE id = :parentId");
query.setParameter("parentId", parentId);
query.executeUpdate();
transaction.commit();
assertNull(
"Parent entity 1 should be deleted", em.find(ParentEntity.class, parentEntity1.getId()));
assertNull(
"Parent entity 2 should be deleted", em.find(ParentEntity.class, parentEntity2.getId()));
assertNull(
"Child entity 1 should be deleted along with the parent",
em.find(ChildEntity.class, childEntity1.getId()));
assertNull(
"Child entity 2 should be deleted along with the parent",
em.find(ChildEntity.class, childEntity2.getId()));
}
}
( https://hibernate.atlassian.net/browse/HHH-17031#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-17031#add-comment?atlOrigin=ey... )
Get Jira notifications on your phone! Download the Jira Cloud app for Android ( https://play.google.com/store/apps/details?id=com.atlassian.android.jira.... ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailN... ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100233- sha1:3ea1a2a )
11 months