gpt4 book ai didi

java - 当我引用复合键时,无法找到具有逻辑名称错误的列

转载 作者:行者123 更新时间:2023-12-04 09:28:28 25 4
gpt4 key购买 nike

我在这里的几个帖子中看到了这个问题。但发现他们都没有帮助我解决它。并且无法跟踪发生此异常的原因。
这是我的实体

@Entity
@Table(name = "iiot_machine")
public class IIOTMachineModel {

@Id
@Column(name = "machine_id")
private String machineId;

@Column(name = "machine_name")
private String machineName;

@Column(name = "machine_description")
private String machineDescription;

@Column(name="created_on", columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
private LocalDateTime createdOn;

<-- Setters and Getters -->
}
具有复合键的复合实体的第二个实体如下
@Entity
@Table(name = "iiot_shift")
public class IIOTShiftModel implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@Column(name = "shift_id")
private String shiftId;

@Id
@ManyToOne
@JoinColumn(name="machine_id", referencedColumnName = "machine_id")
private IIOTMachineModel machine;

@JsonFormat(shape = Shape.STRING, pattern = "HH:mm:ss")
@Column(name = "start_time")
private LocalTime startTime;

@JsonFormat(shape = Shape.STRING, pattern = "HH:mm:ss")
@Column(name = "end_time")
private LocalTime endTime;

@Column(name= "created_on", columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
private LocalDateTime createdOn;

<-- Setters and Getters -->
}
到目前为止,使用上述两个实体,当我运行应用程序时,一切正常。但是,当我添加以下具有“一到五”关系的实体时,会出现错误。实体定义如下
@Entity
@Table(name="iiot_product")
public class IIOTProductModel implements Serializable {

private static final long serialVersionUID = 7807669404047523909L;

@Id
@Column(name = "product_id", length = 100)
private String productId;

@Id
@ManyToOne
@JoinColumn(name="machine_id", referencedColumnName = "machine_id")
private IIOTMachineModel machine;

@Column(name = "line_id")
private String lineId;

private String variant;

@ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
@JoinColumns({
@JoinColumn(name="shift_id", referencedColumnName="shift_id"),
@JoinColumn(name="machine_id", referencedColumnName="machine_id")
})
private IIOTShiftModel shift;
private String operatorName;

<-- Setters and Getters -->
}
在这里使用这个实体,特别是当我用两个键映射 shift 时,我收到了这个错误。如果我在这里遗漏了什么,请告诉我。
完整堆栈跟踪如下
2020-07-16 15:45:20.436  WARN 21496 --- [  restartedMain] 
o.s.b.f.support.DisposableBeanAdapter : Invocation of destroy method
failed on bean with name 'entityManagerFactory':
org.hibernate.MappingException: Unable to find column with logical name:
machine_id in iiot_shift
2020-07-16 15:45:20.436 INFO 21496 --- [ restartedMain]
o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService
'applicationTaskExecutor'
2020-07-16 15:45:20.437 INFO 21496 --- [ restartedMain]
com.zaxxer.hikari.HikariDataSource : HikariPool-64 - Shutdown
initiated...
2020-07-16 15:45:20.450 INFO 21496 --- [ restartedMain]
com.zaxxer.hikari.HikariDataSource : HikariPool-64 - Shutdown
completed.
2020-07-16 15:45:20.576 INFO 21496 --- [ restartedMain]
o.apache.catalina.core.StandardService : Stopping service [Tomcat]

最佳答案

  • 您需要两个主键类
  •    public class IIOTShiftModelPK  implements Serializable {
    private String machine;

    private String shiftId;

    .....getters and setters
    }
       public class IIOTProductModelPK  implements Serializable {
    private String productId;
    private String machine;

    ..getters and setters
    }
  • 使用主键类
  •     @Entity
    @Table(name="iiot_product")
    @IdClass(IIOTProductModelPK.class)
    public class IIOTProductModel implements Serializable {
        @Entity
    @Table(name = "iiot_shift")
    @IdClass(IIOTShiftModelPK.class)
    public class IIOTShiftModel implements Serializable {
  • 更新的产品模型作为主键用于连接到另一个关系的一部分
  •     @Column(name = "shift_id")
    private String shiftId;

    @ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
    @JoinColumns({
    @JoinColumn(name="shift_id", referencedColumnName="shift_id",
    insertable = false, updatable = false),
    @JoinColumn(name="machine_id", referencedColumnName="machine_id",
    insertable = false, updatable = false)
    })
    private IIOTShiftModel shift;

    public IIOTProductModel(String productId, IIOTMachineModel machine,
    String lineId, String variant,
    IIOTShiftModel shift,
    String operatorName) {
    this.productId = productId;
    this.machine = machine;
    this.lineId = lineId;
    this.variant = variant;
    this.shift = shift;
    this.operatorName = operatorName;
    if (Objects.nonNull(shift))
    this.shiftId = shift.getShiftId();
    }

    public void setShift(IIOTShiftModel shift) {
    this.shift = shift;
    this.shiftId = shift.getShiftId();
    }
  • 请参阅带有解决方案的 Github 存储库。 https://github.com/kavi-kanap/stackoverflow-62934039
  • 关于java - 当我引用复合键时,无法找到具有逻辑名称错误的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62934039/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com