Java JPA 存储库一对一联接表错误联接变量和未知列错误

问题描述 投票:0回答:1

我正在努力让一对一的加入关系 JPA 存储库调用正常工作。

我有两个表product和display_name,所以我创建了以下实体

 @Entity
 @Table(name = "product")
 @NoArgsConstructor
 @ToString(callSuper = true)
 @EqualsAndHashCode(callSuper = true, of = "id")
 @Getter
 @Setter
 public class Product {
 
   @Builder
   public Product(
       UUID id,
       ...
       String name) {
     this.id = id
     ...
     this.name = name;
   }
 
   @Id
   @Column(name = "id", nullable = false, updatable = false, unique = true)
   protected UUID id;
 
   ...
 
   @Column(name = "name", nullable = false)
   private String name;
 }
 @Entity
 @Table(name = "display_name")
 @NoArgsConstructor
 @ToString()
 @Getter
 @Setter
 public class DisplayName {
 
   @Builder
   public DisplayName(
       String systemName,
       String displayName) {
     this.systemName = systemName;
     this.displayName = displayName;
   }
 
   @Id
   @Column(name = "system_name", nullable = false)
   private String systemName;
 
   @Column(name = "display_name", nullable = false)
   private String displayName;

现在我想使用我的 JPA 产品存储库来执行类似于此查询的操作,以获取两个表数据:

 SELECT * FROM product
 LEFT JOIN display_name
 ON name = system_name;

这是一对一的关系,意味着 Product 中的每个名称都映射到一个 system_name,也映射到另一个 system_name 所以我添加了一些外键(不确定是否需要)

ALTER TABLE display_name
 ADD CONSTRAINT fk_system_name
 FOREIGN KEY (system_name) REFERENCES product (`name`) ON DELETE CASCADE;
 
 ALTER TABLE product
 ADD CONSTRAINT fk_name
 FOREIGN KEY (`name`) REFERENCES display_name (system_name) ON DELETE CASCADE;

然后我在产品中为 DisplayName 添加了一对一字段

   @OneToOne()
   @JoinTable(
       name = "display_name",
       joinColumns = {@JoinColumn(name = "system_name")},
       inverseJoinColumns = {@JoinColumn(name = "name")})
   @Exclude
   private DisplayName displayName;

所以现在我有了这个 jpa 存储库,我想从中获取所有产品值

 public interface ProductRepository
     extends JpaRepository<Product, UUID>, JpaSpecificationExecutor<Product> {
         Page<Product> findAllByName(String name);
     }

但是当我在休息控制器中调用 findAllByName 时,出现以下错误

2024-11-16 10:38:07 2024-11-16 15:38:07.706 ERROR 1 --- [io-10005-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.dao.InvalidDataAccessResourceUsageException: JDBC exception executing SQL [
 select p1_0.id,p1_0.name,p1_2.name from product p1_0 left join display_name p1_2 on p1_0.id=p1_2.system_name [(conn=76324) Unknown column 'p1_2.name' in 'field list'] [n/a]; SQL [n/a]] with root cause [] 
 2024-11-16 10:38:07 
 2024-11-16 10:38:07 java.sql.SQLSyntaxErrorException: (conn=76324) Unknown column 'p1_2.name' in 'field list'

我不确定为什么它会在

product.id
上加入
display_name.system_name
并在不退出时寻找
display_name.name
?我可能设置了
@OneToOne
字段错误,但我不太确定正确的语法应该是什么。感谢您提前提供帮助

java mysql spring-boot hibernate jpa
1个回答
0
投票

像这样修改你的代码:

// Product entity
   @OneToOne(mappedBy = "product", cascade = CascadeType.ALL)
   @JsonManagedReference
   private DisplayName displayName;

// DisplayName entity
    @OneToOne
    @JsonBackReference
    @ToString.Exclude
    private Product product;


 public interface ProductRepository
     extends JpaRepository<Product, UUID> {
         Page<Product> findAllByName(String name);
     }
© www.soinside.com 2019 - 2024. All rights reserved.