映射来自第三方库的值对象 [英] Mapping value objects from third party libraries

查看:111
本文介绍了映射来自第三方库的值对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下需要通过Hibernate 注解持久化到关系数据库的实体:

  @Entity 
@Table(name =fizzes)
public class Fizz {
@Id @GeneratedValue
@Column(name =fizz_id)
private int id;

@Column(name =fizz_wooz)
private String wooz;

// ???这里我不确定!
私人Buzz嗡嗡声;

//构造函数,getters / setters在这里等等...
}

public class Buzz {
private int jupiter;

私人字符串海王星;

//构造函数,getters / setters在这里,等等...
}

Buzz 的问题是:


  • strong> not 希望它成为它自己的实体/表。我希望它是一个值对象/类型,它被映射到 fizzes 表(通过向其添加列)
  • Buzz 来自第三方库,因此我无法修改它



因此,我要找的表格的最终结果是:

  [fizzes] table 
==============
fizz_id,PRIMARY KEY AUTO INCREMENT
fizz_wooz,NVARCHAR(50)NOT NULL
fizz_buzz_jupiter,INT NOT NULL
fizz_buzz_neptune,NVARCHAR(100)NOT NULL

如何让Hibernate执行这种基于注解的映射当我无法修改 Buzz

解决方案

for称为可嵌入

  @Entity 
公共类Fizz {
...

@嵌入
私人Buzz嗡嗡声;


你可以定义一个映射文件 em> Buzz :

 < entity-mappings version = 1.0xmlns =http://java.sun.com/xml/ns/persistence/ormxmlns:xsi =http://www.w3.org/2001/XMLSchema-instance
xsi :schemaLocation =http://java.sun.com/xml/ns/persistence/orm
http://java.sun.com/xml/ns/persistence/orm_1_0.xsd\">
< embeddable class =... Buzz>
<属性>
< basic name =jupiter>< column name =fizz_buzz_jupiter/>< / basic>
< basic name =neptune>< column name =fizz_buzz_neptune/>< / basic>
< / attributes>
< / embeddable>
< / entity-mappings>

您可以将该映射文件包含在 persistence.xml中

 < persistence-unit> 
< mapping-file> ... / orm.xml< / mapping-file>
< / persistence-unit>

如果您真的想为 Buzz :您无法为其他类定义注释。这就是注释的含义:它们是 inline 并属于它们的类。否则,与映射文件相比,没有任何好处...



但是,您可以扩展 Buzz 并使用该文件

  @Entity 
公共类Fizz {
。 ..

@Embedded
私人BuzzExtension buzz;


$ b @Embeddable
@Access(AccessType.PROPERTY)
public class BuzzExtension extends Buzz {
@Column(name = fizz_buzz_jupiter)
public int getJupiter(){
return super.getJupiter();

$ b @Column(name =fizz_buzz_neptune)
public String getNeptune(){
return super.getNeptune();
}

}

唯一缺点:您不能在 Fizz 中使用 Buzz 的实例。


I have the following entity I need to persist to a relational DB via Hibernate annotations:

@Entity
@Table(name="fizzes")
public class Fizz {
    @Id @GeneratedValue
    @Column(name="fizz_id")
    private int id;

    @Column(name="fizz_wooz")
    private String wooz;

    // ??? here I am unsure!
    private Buzz buzz;

    // Constructor, getters/setters down here, etc...
}

public class Buzz {
    private int jupiter;

    private String neptune;

    // Constructor, getters/setters down here, etc...
}

The problem with Buzz is:

  • I do not want it to be its own entity/table. I want it to be a "value object/type" that gets mapped to the fizzes table (by way of adding columns to it)
  • Buzz is from a third party library and hence I can't modify it

Hence the final result, in table form, that I'm looking for is:

[fizzes] table
==============
fizz_id, PRIMARY KEY AUTO INCREMENT
fizz_wooz, NVARCHAR(50) NOT NULL
fizz_buzz_jupiter, INT NOT NULL
fizz_buzz_neptune, NVARCHAR(100) NOT NULL

How can I get Hibernate to do this annotation-based mapping when I can't modify Buzz?

解决方案

What you are searching for is called Embeddable.

@Entity
public class Fizz {
    ...

    @Embedded
    private Buzz buzz;

}

And you can define a mapping file just for Buzz:

<entity-mappings version="1.0" xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm    
                        http://java.sun.com/xml/ns/persistence/orm_1_0.xsd">
    <embeddable class="...Buzz">
        <attributes>
            <basic name="jupiter"><column name="fizz_buzz_jupiter"/></basic>
            <basic name="neptune"><column name="fizz_buzz_neptune"/></basic>
        </attributes>
    </embeddable>
</entity-mappings>

You can include that mapping file in your persistence.xml:

<persistence-unit>
    <mapping-file>.../orm.xml</mapping-file>
</persistence-unit>

If you really want to use annotations for Buzz: You can't define annotations for other classes. Thats the meaning of annotations: they are inline and belong to their class. Otherwise there would be no benefit compared to mapping files...

But you could extend Buzz and use that one with property access:

@Entity
public class Fizz {
    ...

    @Embedded
    private BuzzExtension buzz;

}

@Embeddable
@Access(AccessType.PROPERTY)
public class BuzzExtension extends Buzz {
    @Column(name="fizz_buzz_jupiter")
    public int getJupiter() {
        return super.getJupiter();
    }

    @Column(name="fizz_buzz_neptune")
    public String getNeptune() {
        return super.getNeptune();
    }

}

Only drawback: You can't use instances of Buzz in Fizz.

这篇关于映射来自第三方库的值对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆