一个@Service适用于Spring中的多个实体。

huangapple go评论82阅读模式
英文:

One @Service for multiple entities in Spring

问题

我有3个相关的实体:`Tag`、`SubcategoryTag`、`CategoryTag`,我想为所有3个实体创建一个共同的`@Service`。

我的Service应该是什么样子的

public class Tag {
    @Id
    @GeneratedValue(generator = "optimized-sequence")
    @Column(name = "id")
    private Long id;

    @Column(name = "name", nullable = false, unique = true)
    private String name;

    @Enumerated(EnumType.STRING)
    @Column(name = "tag_type")
    private TagType tagType;

    @ManyToMany
    private Set<QuestionTemplate> questionTemplates;
}

Subcategory和Category继承自Tag

@Entity
@Inheritance(strategy = JOINED)
@Table(name = "T_TAG")
public class SubcategoryTag extends Tag {
    @ManyToOne(cascade = CascadeType.ALL, fetch = LAZY)
    private CategoryTag parentTag;
}

@Entity
@Inheritance(strategy = JOINED)
@Table(name = "T_TAG")
public class CategoryTag extends Tag {
    @OneToMany(orphanRemoval = true)
    private Set<SubcategoryTag> subcategoryTagSet;
}
英文:

I have 3 related entities Tag, SubcategoryTag, CategoryTag and I want to create a common @Service for all 3.

How should my Service look?

public class Tag {
    @Id
    @GeneratedValue(generator = &quot;optimized-sequence&quot;)
    @Column(name = &quot;id&quot;)
    private Long id;

    @Column(name = &quot;name&quot;, nullable = false, unique = true)
    private String name;

    @Enumerated(EnumType.STRING)
    @Column(name = &quot;tag_type&quot;)
    private TagType tagType;

    @ManyToMany
    private Set&lt;QuestionTemplate&gt; questionTemplates;
}

Subcategory and Category inherit from Tag

@Entity
@Inheritance(strategy = JOINED)
@Table(name = &quot;T_TAG&quot;)
public class SubcategoryTag extends Tag {
    @ManyToOne(cascade = CascadeType.ALL, fetch = LAZY)
    private CategoryTag parentTag;
}

@Entity
@Inheritance(strategy = JOINED)
@Table(name = &quot;T_TAG&quot;)
public class CategoryTag extends Tag {
    @OneToMany(orphanRemoval = true)
    private Set&lt;SubcategoryTag&gt; subcategoryTagSet;
}

答案1

得分: 0

创建三个实体的存储库

public interface TagRepo extends JpaRepository<Tag, Long>{}
public interface SubcategoryRepo extends JpaRepository<Subcategory, Long>{}
public interface CategoryTagRepo extends JpaRepository<CategoryTag,Long>{}

然后添加服务类并自动装配这三个存储库。

@Service
public class TagService {
    @Autowired
    TagRepo tagRepo;

    @Autowired
    SubcategoryRepo subcategoryRepo;

    @Autowired
    CategoryTagRepo categoryTagRepo;

    //添加您的方法
}
英文:

Create repositories for the three entities

public interface TagRepo extends JpaRepository&lt;Tag, Long&gt;{}

public interface SubcategoryRepo extends JpaRepository&lt;Subcategory, Long&gt;{}

public interface CategoryTagRepo extends JpaRepository&lt;CategoryTag,Long&gt;{}

then add the service class and Autowire the three repositories.

@Service
public class TagService {
    @Autowired
    TagRepo tagRepo;

    @Autowired
    SubcategoryRepo subcategoryRepo;

    @Autowired
    CategoryTagRepo categoryTagRepo;

    //Add your methods
    }

huangapple
  • 本文由 发表于 2020年6月29日 15:05:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/62632880.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定