如何在一个表中存储来自不同对象的值

时间:2013-10-09 12:59:38

标签: grails gorm

在我的代码中我有一个带有一些字符串字段的'Book'类和'BookKey'字段,它有一些静态方法,只有一个字符串有'String'值。我的问题是如何只存储这个'key'值(不是BookKey ID的关系),'Book'表中的值?

我不想在我的数据库中创建表“BookKey”。我只想要带有列的'Book'表:'Key','Title'和'Description',其中'Key'是BookKey对象的String值。 我知道如果我不想创建表,我必须使用静态transients = ['key'],但我不知道如何在一个表中存储来自不同对象的值。

class BookKey{
    public final String key;

    public BookKey(){
        this.key="key-undefined"
    }

    public BookKey(String key){
        this.key=key;
    }

    static BookKey generate(){
        String uuid = UUID.randomUUID().toString();
        String key="book-"+uuid;
        return new BookKey(key)
    }

    static BookKey from(String key){
        return new BookKey(key)
    }
}

public class Book {
    BookKey key=BookKey.generate();
    String title;
    String description;

    static transients = ['key']

    static mapping = {
        key column: 'bKey'
    }
}

4 个答案:

答案 0 :(得分:2)

将BookKey类放在src/groovy中,并将您的域类配置为使用embedded。来自文档的示例:

class Person {
    String name
    Country bornInCountry
    Country livesInCountry

    static embedded = ['bornInCountry', 'livesInCountry']
}

// If you don't want an associated table created for this class, either
// define it in the same file as Person or put Country.groovy under the
// src/groovy directory.
class Country {
    String iso3
    String name
}

答案 1 :(得分:2)

您应该查看Grails域类的embedded属性:

 static embedded = ['key']

有关详细信息,请参阅Composition in GORM

答案 2 :(得分:0)

我处理过同样的任务。我从DroolsGormPlugin impl得到了一个想法。我们的想法是使用独特的 BusinessKey 。恩。 id = 123的图书有“ book @ 123 ”BusinessKey&& id ='q23s'的作者有'作者@ q23s 'BusinessKey。然后这些BusinessKeys可以在任何地方使用。例如:

class LogDomain{
  String businessKey
  ...
}  

你不需要每个Damains都有关键表。 Spring Security中使用另一种方法来确定ACL。

答案 3 :(得分:0)

使用static embedded是可以的,但在我的情况下,最好定义我自己的"user-type"并将其添加到GORM映射。

我的类中只有一个带有随机生成的业务键的String字段,因此我想将此值存储为db中的varchar。解决方案是:

Config.groovy中定义:

grails.gorm.default.mapping={
  "user-type" (type: my.package.persistence.PersistentBookKey, class: BookKey)
}

创建实现PersistentBookKey接口的类UserType并创建一些方法:

final class PersistentBookKey implements UserType
{
    @Override
    protected BookKey createKeyFromString(String key) {
        BookKey.from(key)
    }

    @Override
    Class returnedClass() {
        BookKey
    }

    private static int [] SQL_TYPES=[Types.VARCHAR] as int[]

    @Override
    int[] sqlTypes() {
        return SQL_TYPES
    }

    @Override
    boolean equals(Object x, Object y) throws HibernateException {
        return x.equals(y);
    }

    @Override
    int hashCode(Object x) throws HibernateException {
        return x.hashCode()
    }

    @Override
    Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
        def key=rs.getString(names[0])
        return this.createKeyFromString(key);
    }

    @Override
    void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
        BookKey persistent=value as BookKey
        st.setString(index,persistent?.getKey())
    }

    @Override
    Object deepCopy(Object value) throws HibernateException {
        return value
    }

    @Override
    boolean isMutable() {
        return false  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    Serializable disassemble(Object value) throws HibernateException {
        return value as Serializable
    }

    @Override
    Object assemble(Serializable cached, Object owner) throws HibernateException {
        return cached
    }

    @Override
    Object replace(Object original, Object target, Object owner) throws HibernateException {
        return original
    }


}

现在,BookKey对象作为Varchar存储在数据库中,但是当我得到它们时,它们将被转换回BookKey对象。

有关更多信息,请查看此处:

http://grails.org/doc/2.0.x/ref/Database%20Mapping/Usage.html

http://grails.1312388.n4.nabble.com/Working-example-of-user-type-td1377468.html