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-7135 Tests for derived identity with joined inheritance #9610

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,11 @@ private JoinColumn[] getJoinColumnAnnotations(MemberDetails property, PropertyDa
*/
AnnotatedColumns overrideColumnFromMapperOrMapsIdProperty(PropertyData override) {
if ( override != null ) {
final OneToOne oneToOneAnn = override.getAttributeMember().getDirectAnnotationUsage( OneToOne.class );
if ( oneToOneAnn != null && nullIfEmpty( oneToOneAnn.mappedBy() ) != null ) {
throw new AnnotationException( "Property '" + getPath( propertyHolder, override )
+ "' is annotated '@MapsId' but has no columns due to its '@OneToOne(mappedBy)' usage" );
}
final AnnotatedJoinColumns joinColumns = buildExplicitJoinColumns( override.getAttributeMember(), override );
return joinColumns == null
? buildDefaultJoinColumnsForToOne( override.getAttributeMember(), override )
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.annotations.derivedidentities;

import jakarta.persistence.Basic;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Inheritance;
import jakarta.persistence.InheritanceType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToOne;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.Jira;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;


@DomainModel(
annotatedClasses = {
DerivedIdentityJoinedInheritanceTest.ThingHolder.class, DerivedIdentityJoinedInheritanceTest.AThing.class, DerivedIdentityJoinedInheritanceTest.Thing1.class, DerivedIdentityJoinedInheritanceTest.Thing2.class
}
)
@SessionFactory
@Jira("https://hibernate.atlassian.net/browse/HHH-7135")
public class DerivedIdentityJoinedInheritanceTest {

@Test
public void testInsertIntoMap(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
ThingHolder holder = new ThingHolder();
Thing1 thing1 = new Thing1( holder, "test" );
session.persist( holder );
session.persist( thing1 );
}
);

}

@Entity(name = "AThing")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class AThing {

Check notice

Code scanning / CodeQL

Inner class could be static Note test

AThing should be made static, since the enclosing instance is not used.
public AThing() {
}

public AThing(ThingHolder holder) {
this.holder = holder;
}

@Id
@OneToOne
@JoinColumn(name = "id")
private ThingHolder holder;

public ThingHolder getHolder() {
return holder;
}
}

@Entity(name = "ThingHolder")
public class ThingHolder {

Check notice

Code scanning / CodeQL

Inner class could be static Note test

ThingHolder should be made static, since the enclosing instance is not used.
public ThingHolder() {
}

@Id
@GeneratedValue
private Integer id;
@OneToOne
private AThing thing;

public Integer getId() {
return id;
}

public AThing getThing() {
return thing;
}

public void setThing(AThing thing) {
this.thing = thing;
}
}

@Entity(name = "Thing1")
public class Thing1 extends AThing {

Check notice

Code scanning / CodeQL

Inner class could be static Note test

Thing1 should be made static, since the enclosing instance is not used.
public Thing1() {
super();
}

public Thing1(ThingHolder holder, String string) {
super( holder );
this.string = string;
}

@Basic
private String string;

public String getString() {
return string;
}
}

@Entity(name = "Thing2")
public class Thing2 extends AThing {

Check notice

Code scanning / CodeQL

Inner class could be static Note test

Thing2 should be made static, since the enclosing instance is not used.
public Thing2() {
super();
}

public Thing2(ThingHolder holder, Integer integer) {
super( holder );
this.integer = integer;
}

@Basic
private Integer integer;

public Integer getInteger() {
return integer;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.annotations.derivedidentities;

import jakarta.persistence.Basic;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Inheritance;
import jakarta.persistence.InheritanceType;
import jakarta.persistence.MapsId;
import jakarta.persistence.OneToOne;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.Jira;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;


@DomainModel(
annotatedClasses = {
MapsIdJoinedInheritanceTest.ThingHolder.class, MapsIdJoinedInheritanceTest.AThing.class, MapsIdJoinedInheritanceTest.Thing1.class, MapsIdJoinedInheritanceTest.Thing2.class
}
)
@SessionFactory
@Jira("https://hibernate.atlassian.net/browse/HHH-7135")
public class MapsIdJoinedInheritanceTest {

@Test
public void testInsertIntoMap(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
ThingHolder holder = new ThingHolder();
Thing1 thing1 = new Thing1( holder, "test" );
session.persist( holder );
session.persist( thing1 );
}
);

}

@Entity(name = "AThing")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class AThing {

Check notice

Code scanning / CodeQL

Inner class could be static Note test

AThing should be made static, since the enclosing instance is not used.
public AThing() {
}

public AThing(ThingHolder holder) {
this.holder = holder;
}

@Id
private Integer id;
@MapsId
@OneToOne
private ThingHolder holder;

public Integer getId() {
return id;
}

public ThingHolder getHolder() {
return holder;
}
}

@Entity(name = "ThingHolder")
public class ThingHolder {

Check notice

Code scanning / CodeQL

Inner class could be static Note test

ThingHolder should be made static, since the enclosing instance is not used.
public ThingHolder() {
}

@Id
@GeneratedValue
private Integer id;
@OneToOne
private AThing thing;

public Integer getId() {
return id;
}

public AThing getThing() {
return thing;
}

public void setThing(AThing thing) {
this.thing = thing;
}
}

@Entity(name = "Thing1")
public class Thing1 extends AThing {

Check notice

Code scanning / CodeQL

Inner class could be static Note test

Thing1 should be made static, since the enclosing instance is not used.
public Thing1() {
super();
}

public Thing1(ThingHolder holder, String string) {
super( holder );
this.string = string;
}

@Basic
private String string;

public String getString() {
return string;
}
}

@Entity(name = "Thing2")
public class Thing2 extends AThing {

Check notice

Code scanning / CodeQL

Inner class could be static Note test

Thing2 should be made static, since the enclosing instance is not used.
public Thing2() {
super();
}

public Thing2(ThingHolder holder, Integer integer) {
super( holder );
this.integer = integer;
}

@Basic
private Integer integer;

public Integer getInteger() {
return integer;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.annotations.derivedidentities;

import jakarta.persistence.Basic;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Inheritance;
import jakarta.persistence.InheritanceType;
import jakarta.persistence.MapsId;
import jakarta.persistence.OneToOne;
import org.hibernate.AnnotationException;
import org.hibernate.boot.MetadataSources;
import org.hibernate.testing.orm.junit.Jira;
import org.hibernate.testing.orm.junit.ServiceRegistry;
import org.hibernate.testing.orm.junit.ServiceRegistryScope;
import org.junit.jupiter.api.Test;

import static org.junit.Assert.fail;

@ServiceRegistry
@Jira("https://hibernate.atlassian.net/browse/HHH-7135")
public class MapsIdUnownedOneToOneTest {

@Test
public void testInvalidMapping(ServiceRegistryScope scope) {
MetadataSources metadataSources = new MetadataSources( scope.getRegistry() )
.addAnnotatedClasses( MapsIdUnownedOneToOneTest.ThingHolder.class, MapsIdUnownedOneToOneTest.AThing.class, MapsIdUnownedOneToOneTest.Thing1.class, MapsIdUnownedOneToOneTest.Thing2.class );
try {
metadataSources.buildMetadata();
fail( "Was expecting failure" );
}
catch (AnnotationException ignore) {
}
}

@Entity(name = "AThing")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class AThing {

Check notice

Code scanning / CodeQL

Inner class could be static Note test

AThing should be made static, since the enclosing instance is not used.
public AThing() {
}

public AThing(ThingHolder holder) {
this.holder = holder;
}

@Id
private Integer id;
@MapsId
@OneToOne(mappedBy = "thing")
private ThingHolder holder;

public Integer getId() {
return id;
}

public ThingHolder getHolder() {
return holder;
}
}

@Entity(name = "ThingHolder")
public class ThingHolder {

Check notice

Code scanning / CodeQL

Inner class could be static Note test

ThingHolder should be made static, since the enclosing instance is not used.
public ThingHolder() {
}

@Id
@GeneratedValue
private Integer id;
@OneToOne
private AThing thing;

public Integer getId() {
return id;
}

public AThing getThing() {
return thing;
}

public void setThing(AThing thing) {
this.thing = thing;
}
}

@Entity(name = "Thing1")
public class Thing1 extends AThing {

Check notice

Code scanning / CodeQL

Inner class could be static Note test

Thing1 should be made static, since the enclosing instance is not used.
public Thing1() {
super();
}

public Thing1(ThingHolder holder, String string) {
super( holder );
this.string = string;
}

@Basic
private String string;

public String getString() {
return string;
}
}

@Entity(name = "Thing2")
public class Thing2 extends AThing {

Check notice

Code scanning / CodeQL

Inner class could be static Note test

Thing2 should be made static, since the enclosing instance is not used.
public Thing2() {
super();
}

public Thing2(ThingHolder holder, Integer integer) {
super( holder );
this.integer = integer;
}

@Basic
private Integer integer;

public Integer getInteger() {
return integer;
}
}
}
Loading