GORM many to many and one to many through 1 table

MaxR

New member
I have a problem with describe DB structure into my domain classes with such DB structure

ERD

http://i.stack.imgur.com/LgwQf.png

With other domains this works, but it's more simple, no such many relation one-to-many in one table. But with in this case I got error.

has_access [has_access_id])) must have same number of columns as the referenced primary key (has_access [user_id,song_list_id])

What I have

user can have many song list, for this I created middle table and in this table I added field with access type as id from table access_type, which have some type (all, friend etc.)


Code:
class AccessType {

    static hasMany = [hasAccess: HasAccess]

    String type

    static mapping = {
        hasAccess joinTable:[name:'has_access', key:'access_type_id']
    }
}


class HasAccess {

    static belongsTo = [AccessType, SongList, User]

    static constraints = {
        id composite: ['song_list_id', 'user_id']
    }
}

class User {

    transient springSecurityService

    static hasMany = [songList: SongList, hasAccess: HasAccess]

    ..


    static mapping = {
        password column: '`password`'
        songList joinTable:[name:'has_access', key:'user_id']
    }

}

class SongList {

    static belongsTo = [Song, User]
    static hasMany = [songs: Song, users: User, hasAccess: HasAccess]

    String title
    Date created
    Date updated

    static mapping  = {
        songs joinTable:[name:'has_song', key:'song_list_id']
        users joinTable:[name:'has_access', key:'song_list_id']
    }
}

Thank you.
 

MaxR

New member
I found the solution. I remade all my domains and create middle domains manual.

The result are:

Code:
class User {

	transient springSecurityService

	static hasMany = [songList: SongList, hasAccess: HasAccess]

	String username
	String email
	String password
	boolean enabled = true

	static transients = ['springSecurityService']

	static constraints = {
		username blank: false, unique: true
		email blank: false, unique: true
		password blank: false
	}

	static mapping = {
		password column: '`password`'
	}

	Set<Role> getAuthorities() {
		UserRole.findAllByUser(this).collect { it.role }
	}

	def beforeInsert() {
		encodePassword()
	}

	def beforeUpdate() {
		if (isDirty('password')) {
			encodePassword()
		}
	}

	protected void encodePassword() {
		password = springSecurityService?.passwordEncoder ? springSecurityService.encodePassword(password) : password
	}
}

Code:
class AccessType {

    static hasMany = [hasAccess: HasAccess]

    String type

    static constraints = {
    }

    static mapping = {
        hasAccess joinTable:[name:'has_access', key:'access_type_id']
    }
}

Code:
class HasAccess {

    static belongsTo = [AccessType, User, SongList]

    User user
    AccessType accessType
    SongList songList

    static constraints = {
    }
}

Code:
class SongList {

    static belongsTo = [User]
    static hasMany = [hasAccess: HasAccess, hasSong: HasSong, songSubList: SongSubList]

    String title
    Date created
    Date updated
    User user

    static constraints = {

    }

    static mapping  = {
    }
}

Not final variant but what I want at this moment.
 
Top