Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HHH-15722 : @OneToMany mappedBy with a @Any #8109

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.hibernate.boot.spi.MetadataBuildingContext;
import org.hibernate.dialect.Dialect;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.mapping.Any;
import org.hibernate.mapping.Collection;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.Component;
Expand Down Expand Up @@ -792,6 +793,9 @@ private static List<Column> mappedByColumns(PersistentClass associatedClass, Str
}
return element.getColumns();
}
else if (value instanceof Any) {
return ( (Any) value ).getKeyDescriptor().getColumns();
}
else {
return value.getColumns();
}
Expand Down
4 changes: 4 additions & 0 deletions hibernate-core/src/main/java/org/hibernate/mapping/Any.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ public BasicValue getDiscriminatorDescriptor() {
return discriminatorDescriptor;
}

public BasicValue getKeyDescriptor() {
return keyDescriptor;
}

public MetaValue getMetaMapping() {
return metaMapping;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ public static void addPrefixedPropertyNames(
targetKeyPropertyNames.add( prefix );
}
if ( type.isComponentType() ) {
final ComponentType componentType = (ComponentType) type;
final CompositeType componentType = (CompositeType) type;
final String[] propertyNames = componentType.getPropertyNames();
final Type[] componentTypeSubtypes = componentType.getSubtypes();
for ( int i = 0, propertyNamesLength = propertyNames.length; i < propertyNamesLength; i++ ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.orm.test.mapping.manytoone;

import java.util.Set;

import org.hibernate.Session;
import org.hibernate.annotations.Any;
import org.hibernate.annotations.AnyDiscriminator;
import org.hibernate.annotations.AnyDiscriminatorValue;
import org.hibernate.annotations.AnyKeyJavaClass;
import org.hibernate.cfg.JdbcSettings;

import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.Jpa;
import org.hibernate.testing.orm.junit.Setting;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;

import static jakarta.persistence.DiscriminatorType.STRING;

/**
* @author Vincent Bouthinon
*/
@Jpa(
annotatedClasses = {
ManyToOneWithAnyTest.Library.class,
ManyToOneWithAnyTest.Book.class
},
integrationSettings = @Setting(name = JdbcSettings.SHOW_SQL, value = "true")
)
@JiraKey("HHH-15722")
class ManyToOneWithAnyTest {

@Test
void testMappingManyToOneMappedByAny(EntityManagerFactoryScope scope) {

scope.inTransaction(
entityManager -> {
Library library = new Library();
Book firstBook = new Book();
final Set<Book> books = Set.of( firstBook, new Book() );
library.setBooks( books );

entityManager.persist( library );
entityManager.flush();
entityManager.clear();

firstBook = entityManager.unwrap( Session.class )
.byId( firstBook.getClass() )
.load( firstBook.getId() );

Assertions.assertNotNull( firstBook );

entityManager.clear();

library = entityManager.unwrap( Session.class )
.byId( library.getClass() )
.load( library.getId() );

Assertions.assertNotNull( library );
Assertions.assertEquals( 2, library.getBooks().size() );
}
);
}

@Entity(name = "library")
@Table(name = "TLIBRARY")
public static class Library implements Store {

@Id
@GeneratedValue
private Long id;

@OneToMany(mappedBy = "store", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<Book> books;

public void setId(final Long id) {
this.id = id;
}

public Long getId() {
return id;
}

public Set<Book> getBooks() {
return books;
}

public void setBooks(final Set<Book> books) {
books.forEach( book -> book.setStore( this ) );
this.books = books;
}
}

@Entity(name = "book")
@Table(name = "TBOOK")
public static class Book {

@Id
@GeneratedValue
private Long id;

@Any
@AnyDiscriminator(STRING)
@AnyDiscriminatorValue(discriminator = "library", entity = Library.class)
@AnyKeyJavaClass(Long.class)
@Column(name = "STORE_ROLE")
@JoinColumn(name = "STORE_ID")
private Store store;

public void setId(final Long id) {
this.id = id;
}

public Long getId() {
return id;
}

public Store getStore() {
return store;
}

public void setStore(final Store store) {
this.store = store;
}
}

public interface Store {
}
}