关于实施域类关系的问题Grails中的限制 [英] Questions about implementing Domain class relationships & constraints in Grails
问题描述
使用ArgoUML,我很快就创建了几个域类(Person,Store,Product)及其关系的简单表示。 UML模型http://img411.imageshack.us/img411/715/soquestion.png
我正在努力实现关系。以下是我对Person域的初始方法,但似乎我缺少一些重要的东西。
class PersonToPerson {
个人从
个人到
字符串关系
static constraints = {
relation(inList:[Friend to,Enemy of,Likes,Hates])
}
static belongsTo = [Person]
}
class Person {
String firstName
String secondName
。
。
。
static hasMany = [personToPerson:PersonToPerson,
personToStore:PersonToStore]
}
编辑:更新问题的清晰度
在考虑了这个问题之后我有一个更好的方式来问问题。在上面的PersonToPerson的实现中,我将该关系作为一个简单的字符串。我希望用户能够从对于PersonToPerson的字符串值的约束中定义的唯一关系列表进行选择。所以这会导致问题...
- 应该将personToPerson和personToStore整合到一个关系类型的列表中?或者应该保持独立的列表,如图所示?
- 允许用户在关系约束中添加新值的机制是什么?
1)域模型
保持你的代码简单。不要创建通用数据模型。这是地狱的方式。当您的PersonToPerson和personToStore保持分开时,跟踪您的代码更容易。
实际建议的解决方案使得有可能同时访问关系作为综合和独立的列表。
对于这个问题我将使用
<$ p
$ p> class Person {
String name
static hasMany = [personToPerson:PersonToPerson,
personToProduct:PersonToProduct,
personToStore:PersonToStore]
static mappedBy = [personToPerson:from]
}
class Product {
String productName
}
class关系{
String note
}
class Store {
String storeName
}
class PersonToPerson extends Relationship {
人从
个人到
字符串关系
static constraints = {
relation(inList:[Friend to,Enemy of,Likes,Hates])
}
static belongsTo = [from:Person]
}
class PersonToProduct extends关系{
人物
产品产品
字符串关系
static constraints = {
relation(inList:[likes,dislikes])
}
static belongsTo = [person:Person]
}
class PersonToStore extends Relationship {
Person person
Store store
String relation
static constraints = {
relation(inList:[股票人,所有者,经理,赞助人))
}
static belongsTo = [person:Person]
}
人员,产品和商店的DB模式是通常的。但是关系域如下所示:
关系
code>字段类型Null默认
id bigint(20)否
版本bigint(20)否
注意varchar(255)否
类varchar(255)否
person_id bigint(20)是NULL
product_id bigint(20)是NULL
关系varchar(8)是NULL
from_id bigint(20)是NULL
to_id bigint( 20)是NULL
store_id bigint(20)是NULL
关系域使访问成为可能所有关系域都抛出一个域。
2)约束
只需将 inList 到验证器。比你可以将约束存储在文件或DB中。
请参阅文档或文件示例。
示例如何将约束值存储在DB。首先创建域对象。
class Constrain {
String name
String type
}
比域类看起来如此:
class PersonToPerson extends Relationship {
Person from
Person to
String relation
static constraints = {
relation nullable:false,validator:{val,obj - >
def list = Constrain.findAllByType('person')。collect {it.name}
return list.contains(val)
})
}
static belongsTo = [from:Person]
}
Using ArgoUML, I very quickly created this trivial representation of a few Domain classes (Person, Store, Product) and their relationships. UML Model http://img411.imageshack.us/img411/715/soquestion.png
I'm struggling with the implementation of the relationships. Below was my initial approach for the Person domain, but it seems that I am missing something important.
class PersonToPerson {
Person from
Person to
String relation
static constraints = {
relation(inList:["Friend to", "Enemy of", "Likes", "Hates"])
}
static belongsTo = [ Person ]
}
class Person {
String firstName
String secondName
.
.
.
static hasMany= [ personToPerson:PersonToPerson,
personToStore:PersonToStore ]
}
Edit: updated question for clarity
After thinking on the problem I think I have a better way to ask the question(s). In the implementation of PersonToPerson above I have the relation as a simple string. I want the user to be able to select from a list of unique relations, which are defined in the constraints, for the string value for PersonToPerson. So this leads to the questions...
- Should personToPerson and personToStore be consolidated into one list of type Relationship? Or should they stay independent lists as shown?
- What is the mechanism to allow the user to add new values to the relation constraint?
1) Domain model
Keep your code simple. Don't create generic data model. It's way to hell. When you personToPerson and personToStore keep separate, it's much easier to follow your code.
Actually suggested solution makes it possible to access relations as consolidated and independent list simultaneously.
For this problem I would use inheritance feature in GORM.
Your classes would look like this:
class Person {
String name
static hasMany = [personToPerson:PersonToPerson,
personToProduct:PersonToProduct,
personToStore:PersonToStore]
static mappedBy = [personToPerson:"from"]
}
class Product{
String productName
}
class Relationship{
String note
}
class Store{
String storeName
}
class PersonToPerson extends Relationship{
Person from
Person to
String relation
static constraints = {
relation(inList:["Friend to", "Enemy of", "Likes", "Hates"])
}
static belongsTo = [ from:Person ]
}
class PersonToProduct extends Relationship{
Person person
Product product
String relation
static constraints = {
relation(inList:["likes", "dislikes"])
}
static belongsTo = [ person:Person ]
}
class PersonToStore extends Relationship{
Person person
Store store
String relation
static constraints = {
relation(inList:["Stock person", "Owner", "Manager", "Patron"])
}
static belongsTo = [ person:Person ]
}
DB schema for Person, Product and Store is usual. But for Relational domains look like this:
Relationship
Field Type Null Default
id bigint(20) No
version bigint(20) No
note varchar(255) No
class varchar(255) No
person_id bigint(20) Yes NULL
product_id bigint(20) Yes NULL
relation varchar(8) Yes NULL
from_id bigint(20) Yes NULL
to_id bigint(20) Yes NULL
store_id bigint(20) Yes NULL
Relationship domain makes possible to access all relational domain throw one domain.
2) Constraint
Just switch inList to validator. Than you can store constrain in file or DB. See documentation or file example.
Example how to store constraint values in DB. First create a domain object.
class Constrain{
String name
String type
}
Than the domain class looks:
class PersonToPerson extends Relationship{
Person from
Person to
String relation
static constraints = {
relation(nullable:false, validator:{val, obj ->
def list = Constrain.findAllByType('person').collect{it.name}
return list.contains(val)
})
}
static belongsTo = [ from:Person ]
}
这篇关于关于实施域类关系的问题Grails中的限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!