【问题标题】:Setting OneToOne relationship in entity with inheritance在具有继承的实体中设置 OneToOne 关系
【发布时间】:2022-09-27 18:35:44
【问题描述】:

我创建了抽象类实体(我想创建不同类型的形状):

@Entity
@Inheritance(strategy = TABLE_PER_CLASS)
@Getter
@Setter
@NoArgsConstructor
public abstract class ShapeEntity {
    @Id
    @GeneratedValue(generator = \"system-uuid\")
    @GenericGenerator(name = \"system-uuid\", strategy = \"uuid\")
    private String id;
    @OneToOne
    private ShapeDetailsEntity shapeDetailsEntity;

    public abstract double getArea();

    public abstract double getPerimeter();
}

我想在每个实体表中添加详细信息:

@Entity
@Getter
@Setter
@Table(name = \"shape_details\")
@AllArgsConstructor
public class ShapeDetailsEntity {
    @Id
    @GeneratedValue(generator = \"system-uuid\")
    @GenericGenerator(name = \"system-uuid\", strategy = \"uuid\")
    private String id;
    ...
    @OneToOne(cascade = CascadeType.ALL, mappedBy = \"shapeDetailsEntity\", fetch = FetchType.LAZY)
    private ShapeEntity shapeEntity;

创建实体的逻辑在服务中:

public class ShapeService {
    public ShapeEntity createShape(ShapeType type, List<Double> parameters) {
        switch (type) {
            case CIRCLE:
                return circleEntityRepository.saveAndFlush(new CircleEntity(parameters));
            case SQUARE:
                return squareEntityRepository.saveAndFlush(new SquareEntity(parameters));
            case RECTANGLE:
                return rectangleEntityRepository.saveAndFlush(new RectangleEntity(parameters));
            default:
                throw new IllegalArgumentException();
        }
    }

现在对于控制器中的测试,我想创建新实体 - 在 cmets 中我将响应放在控制台中:

@PostMapping
public ResponseEntity<String> post(@Valid @RequestBody ShapeRequestModel shapeRequestModel) {
    ShapeEntity shapeEntity = shapeService.createShape(ShapeType.valueOf(shapeRequestModel.getType()), shapeRequestModel.getParameters());
    ShapeDetailsEntity shapeDetailsEntity = shapeService.createShapeDetails(shapeEntity);
    System.out.println(shapeDetailsEntity.getShapeEntity().toString()); // -> CircleEntity{radius=4.5}
    System.out.println(shapeDetailsEntity); // -> ShapeDetailsEntity{all details...}
    System.out.println(shapeEntity.getShapeDetailsEntity().toString()); // -> java.lang.NullPointerException: null

    return new ResponseEntity<>(shapeEntity.toString(), HttpStatus.CREATED);
}

shapeService.createShapeDetails(shapeEntity)好像:

 public ShapeDetailsEntity createShapeDetails(ShapeEntity shapeEntity) {
    ShapeDetailsEntity shapeDetailsEntity = new ShapeDetailsEntity();
    shapeDetailsEntity.setShapeEntity(shapeEntity);
    return shapeDetailsEntityRepository.saveAndFlush(shapeDetailsEntity);
}

我应该如何正确地做才能不为空shapeEntity.getShapeDetailsEntity().toString())?在数据库的地方,什么时候应该是 shapeDetailsEntity 的 id,我得到了空值。

    标签: java spring hibernate relational-database one-to-one


    【解决方案1】:

    您正在创建的ShapeEntity 没有分配ShapeDetailsEntity,即该字段为空,这就是您获得NPE 的原因。如果您不希望这样,则必须为该字段分配一个对象。

    IMO,这个shapeService.createShapeDetails(shapeEntity); 代码不应该是必需的。您的 ShapeService.createShape 方法应将 ShapeDetailsEntity 对象分配给该字段。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-03
      • 1970-01-01
      • 2016-09-23
      相关资源
      最近更新 更多