Reputation: 503
I am having trouble using the @MappedSuperclass
annotation.
Here is a slimmed-down version that demonstrates my confusion.
Superclass:
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
@MappedSuperclass
public class FooBase {
@Id
protected Long id;
// Getters/setters.
}
Subclass:
import javax.persistence.Entity;
@Entity
public class Foo extends FooBase {
String bar;
// Getters/setters.
}
I must be missing something here. Even IntelliJ reports the Foo
class
Persistent entity 'Foo' should have primary key
And when I call JpaRepository#findAll()
, the fields in Foo
are not present in the JSON response entity.
[
{
"id": 1,
}
]
Upvotes: 1
Views: 8401
Reputation: 503
Thanks to all who answered my question.
My problem was not defining getters for subclass fields. Very silly mistake.
Upvotes: 5
Reputation: 39
usually that message
Persistent entity 'Foo' should have primary key
is received when the classes are not registered in the persistence.xml file. while some implementations do not even require persistence.xml file i have noticed that intelij will complain about it if its not there.
so i think creating a persistence.xml file (if you dont have one already) in your resources folder and set the following content
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.1"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="primary">
<class>com.your.company.Foo</class>
<class>com.your.company.Bar</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
</persistence-unit>
</persistence>
Upvotes: 1
Reputation: 441
Try to set bar
property to private
.
Object returned by JPA call is probably proxy object. It's possible that the bar
property is accessed directly instead of using getter when serialized to JSON.
Upvotes: 1
Reputation: 328
In order for a subclass to inherit fields from a MappedSuperClass, the access modifier must be at least protected or in any case, public.
Upvotes: 1
Reputation: 330
Check your pom file, it should look something like this:
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>net.javaguides.hibernate</groupId>
<artifactId>hibernate-tutorial</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>hibernate-mappedsuperclass-example</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.7.Final</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Upvotes: -1