当我从customerorderentity中删除一对一关系时,下一个示例就会出现问题。我的测试顺利通过,但是当我恢复关系时,它失败了,告诉我不能将字符串分配给整数。我的代码中有什么错误?我该怎么解决?
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Accessors(chain = true)
@Table(name = "cust_order")
@EqualsAndHashCode(callSuper = true, onlyExplicitlyIncluded = true)
@NamedEntityGraphs({
@NamedEntityGraph(
name = CustomerOrderEntity.GRAPH_WITH_LINES,
attributeNodes = {
@NamedAttributeNode(value = "lines", subgraph = "subgraph.alternatives")
},
subgraphs = {
@NamedSubgraph(
name = "subgraph.alternatives",
attributeNodes = @NamedAttributeNode(value = "alternatives")
)
}
),
@NamedEntityGraph(
name = CustomerOrderEntity.GRAPH_WITH_DELIVERY,
attributeNodes = {
@NamedAttributeNode(value = "delivery")
}
),
@NamedEntityGraph(
name = CustomerOrderEntity.GRAPH_WITH_ALL_PROPS,
attributeNodes = {
@NamedAttributeNode(value = "lines", subgraph = "subgraph.alternatives"),
@NamedAttributeNode("delivery")
},
subgraphs = {
@NamedSubgraph(
name = "subgraph.alternatives",
attributeNodes = @NamedAttributeNode(value = "alternatives")
)
}
)
})
public class CustomerOrderEntity extends Auditable implements Serializable {
private static final long serialVersionUID = 1L;
public static final String GRAPH_WITH_LINES = "graph.CustomerOrderEntity.lines";
public static final String GRAPH_WITH_DELIVERY = "graph.CustomerOrderEntity.delivery";
public static final String GRAPH_WITH_ALL_PROPS = "graph.CustomerOrderEntity.all";
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "hash_generator")
@GenericGenerator(
name = "hash_generator",
strategy = "com.persistence.generator.HashSequenceGenerator",
parameters = {
@org.hibernate.annotations.Parameter(name = HashSequenceGenerator.EXTRACT_FIELDS_HASHING, value = "tenantId,fulfilLocationId,fulfilOrderId"),
@org.hibernate.annotations.Parameter(name = HashSequenceGenerator.EXTRACT_FIELDS_FORMAT, value = HashSequenceGenerator.EXTRACT_FIELDS_FORMAT_HEX_VALUE)
}
)
@Column(name = "cust_order_key", nullable = false)
private String custOrderKey;
@Column(name = "tenant_id")
private Integer tenantId;
@Column(name = "fulfil_location_id")
private Integer fulfilLocationId;
@Column(name = "fulfil_order_id")
private String fulfilOrderId;
@OneToMany(
targetEntity = CustomerOrderLineEntity.class,
mappedBy = "order",
fetch = FetchType.LAZY
)
@OrderColumn(name = "fulfil_order_line_id")
private List<CustomerOrderLineEntity> lines;
@OneToOne(
targetEntity = CustomerDeliveryEntity.class,
mappedBy = "order",
fetch = FetchType.LAZY
)
private CustomerDeliveryEntity delivery;
}
@Entity
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@Table(name = "cust_order_line")
@ToString(onlyExplicitlyIncluded = true)
@EqualsAndHashCode(onlyExplicitlyIncluded = true, callSuper = true)
public class CustomerOrderLineEntity extends Auditable implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "hash_generator")
@GenericGenerator(
name = "hash_generator",
strategy = "com.persistence.generator.HashSequenceGenerator",
parameters = {
@org.hibernate.annotations.Parameter(name = HashSequenceGenerator.EXTRACT_FIELDS_HASHING, value = "tenantId,fulfilLocationId,fulfilOrderId,fulfilOrderLineId"),
@org.hibernate.annotations.Parameter(name = HashSequenceGenerator.EXTRACT_FIELDS_FORMAT, value = HashSequenceGenerator.EXTRACT_FIELDS_FORMAT_HEX_VALUE)
}
)
@Column(name = "cust_order_line_key", nullable = false)
@ToString.Include
@EqualsAndHashCode.Include
private String custOrderLineKey;
@Column(name = "tenant_id")
@ToString.Include
@EqualsAndHashCode.Include
private Integer tenantId;
@Column(name = "fulfil_location_id")
@ToString.Include
@EqualsAndHashCode.Include
private Integer fulfilLocationId;
@Column(name = "fulfil_order_id")
@ToString.Include
@EqualsAndHashCode.Include
private String fulfilOrderId;
@Column(name = "fulfil_order_line_id")
@ToString.Include
@EqualsAndHashCode.Include
private Integer fulfilOrderLineId;
@ManyToOne(
targetEntity = CustomerOrderEntity.class,
fetch = FetchType.LAZY
)
@JoinColumns({
@JoinColumn(name = "tenant_id", referencedColumnName = "tenant_id", updatable = false, insertable = false),
@JoinColumn(name = "fulfil_location_id", referencedColumnName = "fulfil_location_id", updatable = false, insertable = false),
@JoinColumn(name = "fulfil_order_id", referencedColumnName = "fulfil_order_id", updatable = false, insertable = false)
})
private CustomerOrderEntity order;
@OneToMany(
targetEntity = CustomerOrderLineProdAltEntity.class,
mappedBy = "line",
fetch = FetchType.LAZY
)
private List<CustomerOrderLineProdAltEntity> alternatives;
}
@Entity
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@Table(name = "order_line_prod_alt")
@ToString(callSuper = true, onlyExplicitlyIncluded = true)
@EqualsAndHashCode(callSuper = true, onlyExplicitlyIncluded = true)
public class CustomerOrderLineProdAltEntity extends Auditable implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "order_line_prod_alt_key", nullable = false)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "hash_generator")
@GenericGenerator(
name = "hash_generator",
strategy = "com.persistence.generator.HashSequenceGenerator",
parameters = {
@org.hibernate.annotations.Parameter(name = HashSequenceGenerator.EXTRACT_FIELDS_HASHING, value = "tenantId,fulfilLocationId,fulfilOrderId,fulfilOrderLineId,altGtin"),
@org.hibernate.annotations.Parameter(name = HashSequenceGenerator.EXTRACT_FIELDS_FORMAT, value = HashSequenceGenerator.EXTRACT_FIELDS_FORMAT_HEX_VALUE)
}
)
@ToString.Include
@EqualsAndHashCode.Include
private String orderLineProdAltKey;
@Column(name = "tenant_id")
@ToString.Include
@EqualsAndHashCode.Include
private Integer tenantId;
@Column(name = "fulfil_location_id")
@ToString.Include
@EqualsAndHashCode.Include
private Integer fulfilLocationId;
@Column(name = "fulfil_order_id")
@ToString.Include
@EqualsAndHashCode.Include
private String fulfilOrderId;
@Column(name = "fulfil_order_line_id")
@ToString.Include
@EqualsAndHashCode.Include
private Integer fulfilOrderLineId;
@ManyToOne(
targetEntity = CustomerOrderLineEntity.class,
fetch = FetchType.LAZY
)
@JoinColumns({
@JoinColumn(name = "tenant_id", referencedColumnName = "tenant_id", updatable = false, insertable = false),
@JoinColumn(name = "fulfil_location_id", referencedColumnName = "fulfil_location_id", updatable = false, insertable = false),
@JoinColumn(name = "fulfil_order_id", referencedColumnName = "fulfil_order_id", updatable = false, insertable = false),
@JoinColumn(name = "fulfil_order_line_id", referencedColumnName = "fulfil_order_line_id", updatable = false, insertable = false)
})
private CustomerOrderLineEntity line;
}
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Accessors(chain = true)
@Table(name = "cust_delivery")
@EqualsAndHashCode(callSuper = true, onlyExplicitlyIncluded = true)
public class CustomerDeliveryEntity extends Auditable implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "hash_generator")
@GenericGenerator(
name = "hash_generator",
strategy = "com.persistence.generator.HashSequenceGenerator",
parameters = {
@org.hibernate.annotations.Parameter(name = HashSequenceGenerator.EXTRACT_FIELDS_HASHING, value = "tenantId,fulfilLocationId,fulfilOrderId"),
@org.hibernate.annotations.Parameter(name = HashSequenceGenerator.EXTRACT_FIELDS_FORMAT, value = HashSequenceGenerator.EXTRACT_FIELDS_FORMAT_HEX_VALUE)
}
)
@Column(name = "cust_delivery_key", nullable = false)
private String custDeliveryKey;
@Column(name = "tenant_id")
private Integer tenantId;
@Column(name = "fulfil_location_id")
private Integer fulfilLocationId;
@Column(name = "fulfil_order_id")
private String fulfilOrderId;
@OneToOne(
targetEntity = CustomerOrderEntity.class,
fetch = FetchType.LAZY
)
@JoinColumns({
@JoinColumn(name = "tenant_id", referencedColumnName = "tenant_id", updatable = false, insertable = false),
@JoinColumn(name = "fulfil_location_id", referencedColumnName = "fulfil_location_id", updatable = false, insertable = false),
@JoinColumn(name = "fulfil_order_id", referencedColumnName = "fulfil_order_id", updatable = false, insertable = false)
})
private CustomerOrderEntity order;
}
下一个 Spring 启动库
@Repository
public interface CustomerOrderEntityRepository extends JpaRepository<CustomerOrderEntity, String>, JpaSpecificationExecutor<CustomerOrderEntity> {
@EntityGraph(CustomerOrderEntity.GRAPH_WITH_LINES)
@Query("select entity from CustomerOrderEntity entity where entity.tenantId = :#{#param.tenantId} and entity.fulfilLocationId = :#{#param.fulfilLocationId} and entity.fulfilOrderId = :#{#param.fulfilOrderId}")
Optional<CustomerOrderEntity> findOneByOrderIdWithLines(CustomerOrderEntity param);
@EntityGraph(CustomerOrderEntity.GRAPH_WITH_DELIVERY)
@Query("select entity from CustomerOrderEntity entity where entity.tenantId = :#{#param.tenantId} and entity.fulfilLocationId = :#{#param.fulfilLocationId} and entity.fulfilOrderId = :#{#param.fulfilOrderId}")
Optional<CustomerOrderEntity> findOneByOrderIdWithDelivery(CustomerOrderEntity param);
@EntityGraph(CustomerOrderEntity.GRAPH_WITH_ALL_PROPS)
@Query("select entity from CustomerOrderEntity entity where entity.tenantId = :#{#param.tenantId} and entity.fulfilLocationId = :#{#param.fulfilLocationId} and entity.fulfilOrderId = :#{#param.fulfilOrderId}")
Optional<CustomerOrderEntity> findOneByOrderIdWithAllProps(CustomerOrderEntity param);
}
接下来的测试是:
测试用例
当我尝试从存储库中检索customerorderentity时,它会给出下一个错误跟踪:
堆栈跟踪
1条答案
按热度按时间ki1q1bka1#
我认为您需要为域对象的属性添加getter和setter。您可以首先为customerorderentity.tenantid添加一个getter和一个setter,以查看当您尝试重现此问题时,这是否会改变行为。