带有@Any属性的Hibernate实体在更新时抛出空指针异常

2q5ifsrm  于 2023-10-23  发布在  其他
关注(0)|答案(1)|浏览(136)

我尝试使用@Any注解来表示多态关系。
为了演示,我创建了3个类:

  • 实现Player接口的PcPlayer
  • ConsolePlayer实现Player接口
  • GameScore包含一个带有@Any的玩家。

我可以创建一个GameScore与球员没有任何麻烦,但当我试图更新它,一个空指针异常发生:
无法调用“org.hibernate.metamodel.mapping.AttributeMapping.getAttributeMetadataAccess()”,因为“org.hibernate.sql.results.graph.Fetchable.asAttributeMapping()”的返回值为null。
这是我的代码

PcPlayer

package com.example.demo;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Getter;
import lombok.Setter;

@Entity
public class PcPlayer implements Player {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    public Long getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    private String username;

}

播放器

package com.example.demo;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Getter;
import lombok.Setter;

@Entity
public class ConsolePlayer implements Player {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    public Long getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    private String username;

}

GameScore

package com.example.demo;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.Any;
import org.hibernate.annotations.AnyDiscriminator;
import org.hibernate.annotations.AnyDiscriminatorValue;
import org.hibernate.annotations.AnyKeyJavaClass;

@Entity
public class GameScore  {
    public Long getId() {
        return id;
    }

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

    public Player getPlayer() {
        return player;
    }

    public void setPlayer(Player player) {
        this.player = player;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Any
    @AnyDiscriminator(DiscriminatorType.STRING)
    @AnyDiscriminatorValue(discriminator = "P", entity = PcPlayer.class)
    @AnyDiscriminatorValue(discriminator = "C", entity = ConsolePlayer.class)
    @AnyKeyJavaClass(Long.class)
    @Column(name ="player_type")
    @JoinColumn(name = "property_id")
    private Player player;  

}

服务

PcPlayer pcPlayer = new PcPlayer();
    pcPlayer.setUsername("Mike");

    pcPlayerRepo.save(pcPlayer);

    GameScore game = new GameScore();
    game.setPlayer(pcPlayer);

    gameScoreRepository.save(game);

    // update some game's attributes

    gameScoreRepository.save(game); // Exception thrown...
gfttwv5a

gfttwv5a1#

目前,我没有尝试更新我的hibernate或Sping Boot 版本,但它可能已经修复:
https://github.com/spring-projects/spring-data-jpa/issues/2935#event-9915627847

相关问题