Hibernate和JPA-通过接口公开的错误映射嵌入式类 [英] Hibernate and JPA - Error Mapping Embedded class exposed through an interface

查看:45
本文介绍了Hibernate和JPA-通过接口公开的错误映射嵌入式类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一组接口,用作API,并从其他模块中引用. 这些接口的一组具体实现,专用于主要"应用程序模块.这些类带有许多注释(JPA以及用于XML序列化的XStream).

We have a set of interfaces, used as an API, and referenced from other modules. A set of concrete implementations of those interfaces, private to the "main" app module. These classes carry a number of annotations (JPA as well as XStream for XML serialization).

我遇到了一个问题.我们有一个用户类,其中有许多与位置有关的字段.我们想将它们汇总到一个Address类中.我们希望数据(暂时)保留在同一表中.该方法是一个嵌入式类.

I've run into a problem. We have a user class which had a number of fields within it related to location. We'd like to roll those up into an Address class. We want the data (for now) to remain in the same table. The approach is an embedded class.

问题在于类型签名必须仅引用其他接口才能满足它们实现的接口.

The problem is that the type signatures must only refer to other interfaces to satisfy the interfaces they implement.

当我尝试保留UserImpl时,出现异常:

When I try to persist a UserImpl, I get the exception:

org.hibernate.MappingException:可以 无法确定以下类型: com.example.Address,在表:User, 对于列: [org.hibernate.mapping.Column(address)]

org.hibernate.MappingException: Could not determine type for: com.example.Address, at table: User, for columns: [org.hibernate.mapping.Column(address)]

示例代码:

interface User {
    int getId();
    String getName();
    Address getAddress();
}

@Entity
class UserImpl implements User {
    int id;
    String name;
    Address address;

    int getId() {
        return id;
    }

    void setId(int id) {
        this.id = id;
    }

    String getName() {
        return name;
    }

    String setName(String name) {
        this.name = name;
    }

    @Embedded
    Address getAddress() {
        return address;
    }

    void setAddress(Address address) {
        this.address = address;
    }
}


interface Address {
    String getStreet();
    String getCity();
    String getState();
    String getZip();
    String getCountry();
}

@Embeddable
class AddressImpl implements Address {
    String street;
    String city;
    String state;
    String zip;
    String country;

    public String getStreet() {
        return street;
    }

    public String getCity() {
        return city;
    }

    public String getState() {
        return state;
    }

    //... etc
}

推荐答案

您可以使用

You can use the @Target Hibernate Annotation (which is a Hibernate-specific extension to the JPA annotations)

@Embedded
@Target(AddressImpl.class)
Address getAddress() {
    return address;
}

这篇关于Hibernate和JPA-通过接口公开的错误映射嵌入式类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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