Java 有没有办法根据tablename设置类的String属性?

Java 有没有办法根据tablename设置类的String属性?,java,spring-data-jpa,Java,Spring Data Jpa,我的数据库中有两个表,位于“retail”模式,具有缩进列集: “蔬菜集团”: “水果集团”: 另外,我在数据库类型product_type中有一个enum作为enum(‘水果’、‘蔬菜’) 这是我的实体: @Data @Entity @Table(schema = "retail", name = "vegetable_group") @SecondaryTables({ @SecondaryTable(schema = "retail", name = "fruit_grou

我的数据库中有两个表,位于“retail”模式,具有缩进列集:

“蔬菜集团”:

“水果集团”:

另外,我在数据库类型product_type中有一个enum作为enum(‘水果’、‘蔬菜’)

这是我的实体:

@Data
@Entity
@Table(schema = "retail", name = "vegetable_group")
@SecondaryTables({
        @SecondaryTable(schema = "retail", name = "fruit_group",
                pkJoinColumns = @PrimaryKeyJoinColumn(name = "id", referencedColumnName = "id")
        )
})
public class Product implements Serializable {

    private enum ProductType {
       vegetable, fruit
    }

    @Id
    @Column(name = "id")
    private String id;

    @Enumerated(EnumType.STRING)
    ProductType productType;

    @Column(name = "value")
    private String value;

}
和简单的产品存储库

public interface ProductRepository extends JpaRepository<Product, String> {

}
公共接口ProductRepository扩展了JpaRepository{
}
我需要根据表,从组合数据中,以某种方式设置product.productType的值

我能简单地做到这一点吗

Upd.


我使用@SecondaryTable,因为我需要使用单个实体从两个表中获取数据。例如,我有一个id为99的产品。我不知道它是蔬菜还是水果。这种过程的常用方法是创建两个实体:水果和蔬菜,并通过id搜索它们。但数据库结构无法更改,因此我使用一个实体产品,它通过唯一id从两个表中获取数据。

我不确定它是否能按您的需要工作,
productRepository.findAll()在这种情况下返回4个实体?可能您缺少了
@SecondaryTable
注释的要点,它应该满足字段,而不是第一个表中的字段,它不会在不同的表中分割数据。是的,它返回4个实体,满足了我的目标,但我也需要根据表定义实体属性值。这些表都没有类型为的字段…在我看来,您有两个完全不同的实体,您试图通过错误地使用
@SecondaryTable
将其硬塞进1个类型。你需要提出你想做什么的问题,而不是问你(不正确的)解决方案。是的,实体是不同的,但我使用SecondaryTable,因为这两个表中的数据是相同的(相同的列)。最后一个问题是为这些实体分配不同的枚举。我需要在这两个表之间进行联合,但jpa不支持它,所以我使用辅助表,但不能分配不同的枚举类型。有人能帮忙吗?
@Data
@Entity
@Table(schema = "retail", name = "vegetable_group")
@SecondaryTables({
        @SecondaryTable(schema = "retail", name = "fruit_group",
                pkJoinColumns = @PrimaryKeyJoinColumn(name = "id", referencedColumnName = "id")
        )
})
public class Product implements Serializable {

    private enum ProductType {
       vegetable, fruit
    }

    @Id
    @Column(name = "id")
    private String id;

    @Enumerated(EnumType.STRING)
    ProductType productType;

    @Column(name = "value")
    private String value;

}
public interface ProductRepository extends JpaRepository<Product, String> {

}