映射来自第三方库的值对象

编程入门 行业动态 更新时间:2024-10-24 06:29:09
本文介绍了映射来自第三方库的值对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下需要通过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 =java.sun/xml/ns/persistence/ormxmlns:xsi =www.w3/2001/XMLSchema-instance xsi :schemaLocation =java.sun/xml/ns/persistence/orm java.sun/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="java.sun/xml/ns/persistence/orm" xmlns:xsi="www.w3/2001/XMLSchema-instance" xsi:schemaLocation="java.sun/xml/ns/persistence/orm java.sun/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.

更多推荐

映射来自第三方库的值对象

本文发布于:2023-10-12 23:23:17,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:第三方   对象

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!