Fields of subclasses are null with @MappedSuperclass and InheritanceType.JOINED strategy in Hibernate 5.4

I’m trying to map JPAs with shared fields between a couple of inherited classes. For instance:

@MappedSuperclass
public abstract class BaseJPA { /* Some fields */ }

@Entity
@Table
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Party extends BaseJPA { 
     /* A discriminator field, and some more fields */ 
}

@Entity
@Table
public class Person extends Party { /* some more fields */ }

This exact setup seemed to work fine in before Hibernate 5.4, but now, all of the fields from BaseJPA are null in the Person class.
Is there something I’m missing?

(I’ve posted the full code here: https://stackoverflow.com/questions/64787013/jpa-hibernate-fields-of-subclasses-are-null-with-mappedsuperclass-and-inherit)

I posted a Hibernate test-case here:
https://github.com/bwbb/hibernate-mappedsuperclass-bug/commit/8cfb2efa06864de14cd99ddaa5a1a87ff75da30a

I checked out your test case and it works fine for me. I added the following to verify if the listener is invoked:

	Person p = entityManager.createQuery( "select p from Person p", Person.class )
			.getSingleResult();
	Assert.assertEquals( Long.valueOf( 3L ), p.getCreatedById() );

Yes, the Person does contain the createdById field, but only by a join.

What I would like is for both the PERSON and PARTY tables to have the created_by_party_id column.

Here’s my real schema (PostgreSQL). Please see the comments for the PARTY_TYPE_CODE and CREATED_BY_PARTY_ID columns.

CREATE TABLE PARTY (
	PARTY_ID integer NOT NULL,
	PARTY_TYPE_CODE integer NOT NULL, -- I'm expecting this column to only be in the base Party class, but available as a field in the Person class, because of the InheritanceType.JOINED strategy
	CREATED_BY_PARTY_ID integer NOT NULL,
	CONSTRAINT PARTY_pk PRIMARY KEY (PARTY_ID),
	FOREIGN KEY (CREATED_BY_PARTY_ID) REFERENCES PARTY (PARTY_ID),
);

CREATE TABLE PERSON (
	PARTY_ID integer NOT NULL,
	FIRST_NAME varchar(40) NOT NULL,
	LAST_NAME varchar(40) NOT NULL,
	CREATED_BY_PARTY_ID integer NOT NULL,   -- I'm expecting this column in both tables, because of @MappedSuperclass
	CONSTRAINT PERSON_pk PRIMARY KEY (PARTY_ID),
	CONSTRAINT PERSON_PARTY_ID_FK FOREIGN KEY (PARTY_ID) REFERENCES PARTY (PARTY_ID),
	FOREIGN KEY (CREATED_BY_PARTY_ID) REFERENCES PARTY (PARTY_ID),
);

So what I would like is for the CREATED_BY_PARTY_ID to be copied (not joined) on every @Entity class in my database.
I still need the InheritanceType.JOINED for other columns (e.g. PARTY_TYPE_CODE), however.

Christian, as I was reviewing this again just, I just realized that this is completely unnecessary…

Thanks for the replies anyway…

1 Like