這最好用像Liquibase遷移工具來完成,而http://grails.org/plugin/database-migration插件可能是你最好是在Grails的,因爲它使用Liquibase並與GORM緊密集成。但這個人很容易手工完成。
我不會用hasMany
因爲你可以很容易地從ProjectMemberships
類管理一切,所以你Users
和Projects
類是
class Users {
String password
String firstName
String lastName
String emailAddress
String username
Company company
.....
}
和
class Projects {
String projectName
String description
Date dateCreated
Date lastUpdated
}
我會去用ProjectMemberships
類使用複合鍵,它需要它實現Serializable
並具有良好的hashCode
和equals
:
import org.apache.commons.lang.builder.HashCodeBuilder
class ProjectMemberships implements Serializable {
Users u
Projects p
boolean equals(other) {
if (!(other instanceof ProjectMemberships)) {
return false
}
other.u?.id == u?.id && other.p?.id == p?.id
}
int hashCode() {
def builder = new HashCodeBuilder()
if (u) builder.append(u.id)
if (p) builder.append(p.id)
builder.toHashCode()
}
static ProjectMemberships get(long userId, long projectId) {
find 'from ProjectMemberships where u.id=:userId and p.id=:projectId',
[userId: userId, projectId: projectId]
}
static ProjectMemberships create(Users u, Projects p, boolean flush = false) {
new ProjectMemberships(u: u, p: p).save(flush: flush, insert: true)
}
static boolean remove(Users u, Projects p, boolean flush = false) {
ProjectMemberships instance = ProjectMemberships.findByUsersAndProjects(u, p)
if (!instance) {
return false
}
instance.delete(flush: flush)
true
}
static void removeAll(Users u) {
executeUpdate 'DELETE FROM ProjectMemberships WHERE u=:u', [u: u]
}
static void removeAll(Projects p) {
executeUpdate 'DELETE FROM ProjectMemberships WHERE p=:p', [p: p]
}
static mapping = {
id composite: ['p', 'u']
version false
}
}
使用ProjectMemberships.create()
添加用戶和項目之間的關係,並ProjectMemberships.remove()
將其刪除。
運行grails schema-export
看到更新的DDL(它將在target/ddl.sql
)。運行project_memberships
表的創建表語句,例如
create table project_memberships (
p_id bigint not null,
u_id bigint not null,
primary key (p_id, u_id)
)
然後用這個SQL來填充它(取決於你的數據庫,你可能需要一個稍微不同的語法):
insert into project_memberships(p_id, u_id) select id, project_lead_id from projects
終於從projects
表中刪除project_lead_id
列。
當然在做任何改變之前做一個數據庫備份。
你可以得到一個用戶的項目
def projects = ProjectMemberships.findAllByUsers(user)*.p
,同樣一個項目的用戶
def users = ProjectMemberships.findAllByProjects(project)*.u
不知何故,我知道,這將是你從他們那裏我會得到一個答案。非常感謝。 – Sap