Giter Club home page Giter Club logo

hhh-11144's Introduction

HHH-11144

orphanRemoval not working when other same-table-relations are initialized

execute as

mvn test

using these entities:

@Entity
@Table(name = "ITEM",
	indexes = @Index(columnList = "CODE", unique = true))
public class Item implements Serializable
{
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue
	protected Long id;

	@Column
	protected String code;

	@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true)
	protected Set<ItemRelation> lowerItemRelations = new LinkedHashSet<>();

	@OneToMany(mappedBy = "child", cascade = CascadeType.ALL, orphanRemoval = true)
	protected Set<ItemRelation> higherItemRelations = new LinkedHashSet<>();

	...
}

@Entity
@Table(name = "ITEM_RELATION",
	indexes = @Index(columnList = "PARENT_ID, CHILD_ID", unique = true))
public class ItemRelation implements Serializable
{
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue
	protected Long id;

	@ManyToOne(optional = false)
	@JoinColumn(name = "PARENT_ID")
	private Item parent;

	@ManyToOne(optional = false)
	@JoinColumn(name = "CHILD_ID")
	private Item child;

	@Column(nullable = false, columnDefinition = "INT DEFAULT 0 NOT NULL")
	private int quantity = 1;

	...
}

when executing:

EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();

tx.begin();

Item item = em.createQuery("select x from Item x where x.code = 'first'", Item.class).getSingleResult();

Set<ItemRelation> lowerItemRelations = item.getLowerItemRelations();
Hibernate.initialize(lowerItemRelations);

// initializing higherItemRelations prevents orphanRemoval to work on lowerItemRelations
Set<ItemRelation> higherItemRelations = item.getHigherItemRelations();
Hibernate.initialize(higherItemRelations);

lowerItemRelations.clear();

tx.commit();
em.close();

no DELETE statement is issued.

If you don't initialize higherItemRelations then everything is working fine.

hhh-11144's People

Contributors

mmariotti avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.