给定客户端类
public class Client {
private String firstName;
private String lastName;
private Address address;
//..//
}
和地址类
public class Address {
private int houseNumber;
private String streetName;
private String city;
private String state;
private String zipCode;
private String country;
//...//
}
我已经创建了地址类,因为它在其他一些类中使用,我不想复制代码。有没有办法,使用JPA,明确地将地址信息显示到我的客户端SQL表而不是创建地址表,并使用主键作为两者之间的链接?
答案 0 :(得分:5)
您不需要OneToOne关系,您可以使用Embeddable类型。
template <typename T>
struct MyClass;
template <std::size_t Dim>
struct MyClass<char(*)[Dim]>
{
static char ** getPtr ()
{ static char ach[Dim]; static char * ret { ach } ; return &ret; }
};
template <std::size_t ...>
struct indexList
{ };
template <std::size_t, typename>
struct iLH;
template <std::size_t N, std::size_t ... Is>
struct iLH<N, indexList<Is...>> : public iLH<(N >> 1), indexList<Is..., N>>
{ };
template <std::size_t ... Is>
struct iLH<0U, indexList<Is...>>
{ using type = indexList<Is...>; };
template <std::size_t N>
struct getIndexList : public iLH<N, indexList<>>
{ };
template <std::size_t N>
using getIndexList_t = typename getIndexList<N>::type;
template <std::size_t N>
class Foo
{
private:
const std::vector<char **> bar = bar_init (getIndexList_t<N>{});
template <std::size_t ... Is>
static std::vector<char **> bar_init (indexList<Is...> const &)
{
std::vector<char **> init { MyClass<char(*)[Is]>::getPtr()... };
return init;
}
};
int main ()
{
Foo<32U> f32;
}
这样您可以使用以下列映射表客户端:
@Entity
public class Client {
@Id
private long id;
private String firstName;
private String lastName;
@Embedded
private Address address;
}
@Embeddable
public class Address {
private int houseNumber;
private String streetName;
private String city;
private String state;
private String zipCode;
private String country;
}
您可以在其表中具有相同列的任何其他实体上重复使用该地址。