我试图使用spring-data-cassandra(1.1.2.RELEASE),而我遇到了JPA用语中缺少可嵌入类型映射的问题。
我有一个像这样的实体类:
@Table
public class Customer {
private UUID id;
private String name;
private Address address;
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
和可嵌入的Address类:
public class Address {
private String address;
private String city;
private String state;
private String zip;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
}
我的cassandra表:
create table customer (
id uuid primary key,
name text,
address text,
city text,
state text,
zip text
);
我希望将Address的属性映射到包含的实体,我不想要一个单独的地址表。在JPA中,我相信我会使用@Embeddable注释。 spring-data-cassandra中是否有类似的构造?
答案 0 :(得分:1)
spring-data-cassandra尚不支持嵌入类型。功能请求位于DATACASS-167。
要嵌入的实体的唯一可能部分是主键。如果您的主键包含多个字段,则可以将这些字段外部化为单独的类,然后使用@PrimaryKey
注释将其用于其中。
Comment.java
@Table("comments")
public class Comment {
@PrimaryKey
private CommentKey pk;
private String text;
}
CommentKey.java
@PrimaryKeyClass
public class CommentKey implements Serializable {
private static final long serialVersionUID = -7871651389236401141L;
@PrimaryKeyColumn(ordinal = 0, type = PrimaryKeyType.PARTITIONED)
private String author;
@PrimaryKeyColumn(ordinal = 1)
private String company;
}
HTH,马克