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

时间:2015-10-16 06:54:54

标签: hibernate jpa

我有以下实体需要通过Hibernate 注释持久保存到关系数据库:

@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...
}

Buzz的问题是:

  • 希望它成为自己的实体/表。我希望它是一个“值对象/类型”,它被映射到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

当我无法修改Buzz时,如何让Hibernate执行基于注释的映射?

2 个答案:

答案 0 :(得分:8)

您要搜索的内容称为Embeddable

@Entity
public class Fizz {
    ...

    @Embedded
    private Buzz buzz;

}

您可以为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>

您可以在persistence.xml

中加入该映射文件
<persistence-unit>
    <mapping-file>.../orm.xml</mapping-file>
</persistence-unit>

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

但你可以扩展Buzz并使用属性访问权限

@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();
    }

}

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

答案 1 :(得分:1)

我想你希望Fizz对象的特性存在于Buzz对象中,这意味着Fizzfizzes的属性应该作为列存在同一张表Fizz

因此,您必须扩展Buzz对象,其中@MappedSuperClass将被注释为@Entity @Table(name="fizzes") public class Fizz extends Buzz { // Fizz properties // No need to add Buzz as a field } @MappedSuperclass public class Buzz { // Buzz properties }

@MappedSuperclass

如果您不添加Buzz注释,则无法实现此目的。即使FizzBuzz扩展,Hibernate也无法识别Fizz的字段,因为Hibernate只会对Buzz中的字段进行反映,但不会对字段进行反映存在于扩展@MappedSuperclass类中,除非您指定Buzz注释。 MappedSuperclass将告诉hibernate也在//Check username //Check if the length is greater than 3 and no username already exists. Several other checks included $('#username').blur(function() { var v=this.value; if(v == "") { addPopover(this,'This field is required!'); $('#username_group').removeClass("has-success").addClass("has-error"); $('#username_feedback').removeClass("glyphicon-ok").addClass("glyphicon-remove"); } else if(username.length < 4) { addPopover(this,'The username must be atleast 4 characters long'); $('#username_group').removeClass("has-success").addClass("has-error"); $('#username_feedback').removeClass("glyphicon-ok").addClass("glyphicon-remove"); } else { $.post('php/check_username_avail.php',{ username : v },function(data){ if(data == "false") { alert('false occurred'); addPopover(this,'Sorry! this username is already taken.'); $('#username_group').removeClass("has-success").addClass("has-error"); $('#username_feedback').removeClass("glyphicon-ok").addClass("glyphicon-remove"); alert('false ended'); } else if(data == "true") { alert('true occurred'); removePopover(this); $('#username_group').addClass("has-success").removeClass("has-error"); $('#username_feedback').addClass("glyphicon-ok").removeClass("glyphicon-remove"); alert('true ended'); } else { alert('else occurred!'); addPopover(this,data); $('#username_group').removeClass("has-success").addClass("has-error"); $('#username_feedback').removeClass("glyphicon-ok").addClass("glyphicon-remove"); alert('else ended'); } }).error(function(){ alert("An error occurred. Unable to validate username"); }); } }); //function to add popover function addPopover(id,message) { alert("in add popover"); $(id).attr("data-toggle","popover"); $(id).attr("data-trigger","focus"); $(id).attr("data-placement","left"); $(id).attr("data-content",message); $(id).popover(); alert('add popover ended'); } //function to remove popover function removePopover(id) { alert("in remove popover"); $(id).removeAttr("data-toggle"); $(id).removeAttr("data-trigger"); $(id).removeAttr("data-placement"); $(id).removeAttr("data-content"); alert("remove popover ended"); } 对象的字段上使用反射。