英文:
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 = "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;
}
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.
专注分享java语言的经验与见解,让所有开发者获益!
评论