使用gorm创建多对多关系映射

作者: admin 日期: 2018-01-05 15:11:21 人气: - 评论: 0

我们现在有个user表还有个company_store表是多对多的关系,就是一个用户可以有多个商家,一个商家可以有多个用户,使用gorm可以很方便的创建多对多关系映射

user类groovy代码

import grails.gorm.annotation.Entity
import groovy.transform.EqualsAndHashCode
import groovy.transform.ToString
@Entity
@SuppressWarnings("GroovyUnusedDeclaration")
@ToString
@EqualsAndHashCode
class User {
   
int id
   String phone
   String name
   int status
   String validCode
   String token
   Date dateCreated
   Date lastUpdated
   Title title
   int type
   int sex
   int confirmation
   int deviceType
   String appVersion
   String agentName
   String agentPhone
   String regionCode
   String remark

   static hasMany = [stores: CompanyStore]

   
@SuppressWarnings("GroovyAssignabilityCheck")
   
static constraints = {
       validCode
nullable: true
       token nullable: true
       appVersion nullable: true
       agentName nullable: true
       agentPhone nullable: true
       regionCode nullable: true
       remark nullable: true
   }

   
@SuppressWarnings("GrUnresolvedAccess")
   
static mapping = {
       stores
lazy: true, joinTable: true
       confirmation defaultValue: 0
       remark type: 'text'
   }
}

主要起作用的是

static hasMany = [stores: CompanyStore]

还有

stores lazy: true, joinTable: true


这样gorm就会自动创建一张中间表user_company_store用来表示两张表的关联关系,但是现在我们遇到了一个变动,我们需要在关联表中加入一个status字段来标识用户和商家的关联关系是否被临时停用,查了gorm好像没有好的方式,只能手写一个user_company_store的实体类了

UserCompanyStore.groovy :

import grails.gorm.annotation.Entity
import groovy.transform.EqualsAndHashCode
import groovy.transform.ToString

@SuppressWarnings("GroovyUnusedDeclaration")
@Entity
@ToString
@EqualsAndHashCode
class UserCompanyStore implements Serializable {
User user
    CompanyStore companyStore
    int status
    @SuppressWarnings(["GroovyAssignabilityCheck", "GrUnresolvedAccess"])
static mapping = {
id composite: ['user', 'companyStore']
table 'user_company_store'
              version false
              user column: 'user_stores_id'
              companyStore column: 'company_store_id'
    }
}

User.groovy 修改 :


import grails.gorm.annotation.Entity
import groovy.transform.EqualsAndHashCode
import groovy.transform.ToString
@Entity
@SuppressWarnings("GroovyUnusedDeclaration")
@ToString
@EqualsAndHashCode
class User {
int id
   String phone
   String name
   int status
   String validCode
   String token
   Date dateCreated
   Date lastUpdated
   Title title
   int type
   int sex
   int confirmation
   int deviceType
   String appVersion
   String agentName
   String agentPhone

   String regionCode

   String remark


   static hasMany = [userStore: UserCompanyStore]
Set<CompanyStore> getStores() {
userStore.findAll { it.status == 1 }.collect { it.companyStore }
}

@SuppressWarnings("GroovyAssignabilityCheck")
static constraints = {
validCode nullable: true
       token nullable: true
       appVersion nullable: true
       agentName nullable: true
       agentPhone nullable: true
       regionCode nullable: true
       remark nullable: true
   }

@SuppressWarnings("GrUnresolvedAccess")
static mapping = {
userStore lazy: true
    confirmation defaultValue: 0
    remark type: 'text'
  }
}

主要是修改两个地方 

 static hasMany = [userStore: UserCompanyStore]

还有

 userStore lazy: true


编写测试方法

@Test
@Transactional
void testUserCompanyStoreDao(){
def user = User.get(1)
user.stores.each {
println(it.id)
}
}

成功输出用户id为1的用户对应的商家id


CompanyStore.groovy修改:

import grails.gorm.annotation.Entity
@Entity
@SuppressWarnings("GroovyUnusedDeclaration")
class CompanyStore {
int id
   String companyCode
   String name
   String address
   int status
   String openDate
   Date dateCreated
   Date lastUpdated
   Time companyOpenTime
   int openState
   Date deadLine
   Integer supportKmProcedure
   Integer systemType
   Integer turnoverRule


   static hasMany = [userStore: UserCompanyStore]
Set<User> getUsers() {
this.userStore.findAll { it.status == 1 }.collect { it.user }
}
@SuppressWarnings(["GroovyAssignabilityCheck", "GrUnresolvedAccess"])
static mapping = {
version false
       id column: 'companyID'
       companyCode column: 'companyCode'
       turnoverRule defaultValue: 0
       userStore lazy: true
   }

@SuppressWarnings("GroovyAssignabilityCheck")
static constraints = {
address nullable: true
       openDate nullable: true
       deadLine nullable: true
       companyOpenTime nullable: true
   }

}

主要是添加了

 static hasMany = [userStore: UserCompanyStore]
Set<User> getUsers() {
this.userStore.findAll { it.status == 1 }.collect { it.user }
}

还有

 userStore lazy: true



测试方法

@Test
@Transactional
void testUserCompanyStoreDao(){
/*   def user = User.get(1)
   user.stores.each {
       println(it.id)
   }*/
   def company = CompanyStore.get(14325)
company.users.each {
println(it.id)
}
}


测试通过CompanyStore对象获得用户列表



相关内容

发表评论
更多 网友评论0 条评论)
暂无评论

Copyright © 2012-2014 我的代码板 Inc. 保留所有权利。

页面耗时0.0203秒, 内存占用1.84 MB, 访问数据库12次

闽ICP备15009223号-1