使用 SpringBoot 的 Hibernate 继承映射 - 抛出 class 异常
Hibernate Inheritance Mapping with SpringBoot - cast class Exception
我有这个对象:
@Entity
@Table(name = "PERSONNE")
@NamedQuery(name = "Personne.findAll", query = "SELECT p FROM Personne p")
@Inheritance(strategy = InheritanceType.JOINED)
@ClassExtractor(PersonneClassExtractor.class)
@NoArgsConstructor
@AllArgsConstructor
@Data
@SuperBuilder
@ToString(of = { "personneId", "perId" })
@EqualsAndHashCode(of = { "personneId", "perId" })
public class Personne implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@ReturnInsert
@Column(name = "PERSONNE_ID")
protected Long personneId;
..
}
还有这个:
@Entity
@Table(name = "ENFANT")
@NamedQuery(name = "Enfant.findAll", query = "SELECT e FROM Enfant e")
@PrimaryKeyJoinColumn(name = "PERSONNE_ID")
@Inheritance(strategy = InheritanceType.JOINED)
@Data
@SuperBuilder
@NoArgsConstructor
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
@Audit
public class Enfant extends Personne {
private static final long serialVersionUID = 1L;
..
}
还有这个:
@Repository
public interface EnfantRepository extends PersonneBaseRepository<Enfant>,
JpaSpecificationExecutor<Enfant> {
@Query("SELECT e FROM Enfant e WHERE e.personneId = ?1")
Enfant findOne(Long enfantId);
..
}
和
@NoRepositoryBean
public interface PersonneBaseRepository<T extends Personne>
extends JpaRepository<T, Long> {
}
但是当我这样做的时候
Enfant enfant = enfantRepo.findOne(7L);
我有这个错误:
class com.mundos.model.persons.Personne cannot be cast to class com.mundos.model.persons.Enfant (com.mundos.model.persons.Personne and com.mundos.model.persons.Enfant are in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @67cae0fe)
我也试过 enfantRepo.findById
结果一样
JpaSpecificationExecutor
合约提供的findOne
方法和下面的实现(`SimpleJpaRepository)在不同的spring-data-jpa 项目版本。
虽然鉴于报告的错误,这还不清楚,但运行时可能正在生成一个与您的多态结构不匹配的动态查询(您可以激活 hibernate SQL 登录以检查这些)。
也就是说,您最好使用 findById
和 getById
现成的替代方法,而不是创建您自己的基于查询的方法。您应该从 JpaRepository
声明中删除自定义 findOne
方法:
@Repository
public interface EnfantRepository extends PersonneBaseRepository<Enfant>, JpaSpecificationExecutor<Enfant> {
// ...
}
然后您可以根据其标识符查询 Enfant
实体:
Enfant enfant = enfantRepo.findById(7L).orElseThrow(() -> new EntityNotFoundException(id));
或者使用 getById
替代方法简单地绕过 Optional
return 值:
Enfant enfant = enfantRepo.getById(7L);
根据 的说法,您的错误可能具有误导性,除非您明确地在代码中进行转换。
并且不确定两者是否在您的代码中的同一个 jar|library 中(取决于您的结构)!! ERROR: in unnamed module of loader
尝试一下,如果 devtools 在您的类路径中,这可能会有所帮助。
public static void main(final String[] args) {
System.setProperty("spring.devtools.restart.enabled", "false");
SpringApplication.run(Application.class, args);
}
或
排除 |移除
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
我有这个对象:
@Entity
@Table(name = "PERSONNE")
@NamedQuery(name = "Personne.findAll", query = "SELECT p FROM Personne p")
@Inheritance(strategy = InheritanceType.JOINED)
@ClassExtractor(PersonneClassExtractor.class)
@NoArgsConstructor
@AllArgsConstructor
@Data
@SuperBuilder
@ToString(of = { "personneId", "perId" })
@EqualsAndHashCode(of = { "personneId", "perId" })
public class Personne implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@ReturnInsert
@Column(name = "PERSONNE_ID")
protected Long personneId;
..
}
还有这个:
@Entity
@Table(name = "ENFANT")
@NamedQuery(name = "Enfant.findAll", query = "SELECT e FROM Enfant e")
@PrimaryKeyJoinColumn(name = "PERSONNE_ID")
@Inheritance(strategy = InheritanceType.JOINED)
@Data
@SuperBuilder
@NoArgsConstructor
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
@Audit
public class Enfant extends Personne {
private static final long serialVersionUID = 1L;
..
}
还有这个:
@Repository
public interface EnfantRepository extends PersonneBaseRepository<Enfant>,
JpaSpecificationExecutor<Enfant> {
@Query("SELECT e FROM Enfant e WHERE e.personneId = ?1")
Enfant findOne(Long enfantId);
..
}
和
@NoRepositoryBean
public interface PersonneBaseRepository<T extends Personne>
extends JpaRepository<T, Long> {
}
但是当我这样做的时候
Enfant enfant = enfantRepo.findOne(7L);
我有这个错误:
class com.mundos.model.persons.Personne cannot be cast to class com.mundos.model.persons.Enfant (com.mundos.model.persons.Personne and com.mundos.model.persons.Enfant are in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @67cae0fe)
我也试过 enfantRepo.findById
结果一样
JpaSpecificationExecutor
合约提供的findOne
方法和下面的实现(`SimpleJpaRepository)在不同的spring-data-jpa 项目版本。
虽然鉴于报告的错误,这还不清楚,但运行时可能正在生成一个与您的多态结构不匹配的动态查询(您可以激活 hibernate SQL 登录以检查这些)。
也就是说,您最好使用 findById
和 getById
现成的替代方法,而不是创建您自己的基于查询的方法。您应该从 JpaRepository
声明中删除自定义 findOne
方法:
@Repository
public interface EnfantRepository extends PersonneBaseRepository<Enfant>, JpaSpecificationExecutor<Enfant> {
// ...
}
然后您可以根据其标识符查询 Enfant
实体:
Enfant enfant = enfantRepo.findById(7L).orElseThrow(() -> new EntityNotFoundException(id));
或者使用 getById
替代方法简单地绕过 Optional
return 值:
Enfant enfant = enfantRepo.getById(7L);
根据
并且不确定两者是否在您的代码中的同一个 jar|library 中(取决于您的结构)!! ERROR: in unnamed module of loader
尝试一下,如果 devtools 在您的类路径中,这可能会有所帮助。
public static void main(final String[] args) {
System.setProperty("spring.devtools.restart.enabled", "false");
SpringApplication.run(Application.class, args);
}
或
排除 |移除
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>