基于复合主键和接口的Spring Hibernate映射

huangapple 未分类评论53阅读模式
英文:

Spring Hibernate mapping based on composite primary key and interface

问题

我在Stackoverflow上进行了搜索,但实际上没有找到我问题的答案。无论如何,以下是我尝试使用Spring构建的大致架构:

ConcreteToolA
-------------
id(pk)
....

ConcreteToolB
-------------
id(pk)
....

Resource
--------
id(pk)
name
...

UsedResource
------------------
resource_id(pk)
tool_id(pk)
tool_type(pk)
quantity
....

在Java中,我有类似这样的代码:

public interface Tool {

}

@Entity
@Table(name = "ConcreteToolA")
public class ConcreteToolA implements Tool {

    @OneToMany
    private List<UsedResource> resources;
    private int toolType = Const.A;

}

@Entity
@Table(name = "ConcreteToolB")
public class ConcreteToolB implements Tool {

    @OneToMany
    private List<UsedResource> resources;
    private int toolType = Const.B;

}

@Enity
@Table(name = "Resource")
public class Resource {

    private String name;

}

@Entity
@Table(name = "UsedResource")
public class UsedResource{

    //one-to-many reference to tool
    private Tool usedBy;
    private int quantity;

}

总之,我想要一种通用的方法来将资源与不同(抽象)实体(例如我的情况中的工具)进行关联。但是在未来可能会出现更多的工具或其他更抽象的实体,它们也可以消耗资源(例如人等)。因此,我想要将关系(UsedResource)与一个接口关联起来,可能需要某种自定义的setter方法?

也许我只是在用词上有些模糊,并且可能已经有多次解释了……如果有人能在这方面帮助我,我会很高兴的。

英文:

I've searched on Stackoverflow but did not really find the answer to my question. Anyways, here is a rough schema of what I'm trying to build with spring:

ConcreteToolA
-------------
id(pk)
....
 
ConcreteToolB
-------------
id(pk)
....

Resource
--------
id(pk)
name
...
 
UsedResource
------------------
resource_id(pk)
tool_id(pk)
tool_type(pk)
quantity
....

And in Java I have something like this:

public interface Tool {

}

@Entity
@Table(name = &quot;ConcreteToolA&quot;)
public class ConcreteToolA implements Tool {

    @OneToMany
    private List&lt;UsedResource&gt; resources;
    private int toolType = Const.A;

}

@Entity
@Table(name = &quot;ConcreteToolB&quot;)
public class ConcreteToolB implements Tool {

    @OneToMany
    private List&lt;UsedResource&gt; resources;
    private int toolType = Const.B;

}

@Enity
@Table(name = &quot;Resource&quot;)
public class Resource {

    private String name;

}

@Entity
@Table(name = &quot;UsedResource&quot;)
public class UsedResource{

    //one-to-many reference to tool
    private Tool usedBy;
    private int quantity;

}

To sum up, I want a generic way to link resources with different (abstract) entities like tools in my case. But in the future there might come up further tools or other more abstract entities that can consume resources (people etc.). Therefore I want to link the relation (UsedResource) with an interface and need probably some kind of custom setter?

Maybe I only used the wording search terms and it was already explain multiple times... If someone could help me here I'd be happy.

huangapple
  • 本文由 发表于 2020年4月5日 01:09:39
  • 转载请务必保留本文链接:https://java.coder-hub.com/61031758.html
匿名

发表评论

匿名网友

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

确定