Hibernate OneToOne带有特定的子句

时间:2011-04-11 13:39:33

标签: java hibernate

如果有人知道如何:

我有一个实体

@Entity
public class AnEntity {
    ...

    private String propertyA;
    private String propertyB;
    private String propertyC;
}

字符串propertyA,propertyB和propertyC存储在不同的表中,其中包含对AnEntity,名称和值字段的引用,如:

@Entity
public class Property {
    ...

    private String name;
    private String value;
}

我可以在AnEntity中使用where子句指定OneToOne连接,我希望有类似的内容:

@Entity
public class AnEntity {
    ...
    @OneToOne(table = "properties")
    @JoinColumn("anentity_id")
    @WhereJoinTable(name = "propertyA")
    private String propertyA;
    ...
}

非常感谢任何帮助!谢谢!

2 个答案:

答案 0 :(得分:3)

我认为最简单的方法是:

@Entity 
public class AnEntity {
    @OneToMany
    @JoinColumn("aentity_id")
    @MapKey(name = "name")
    private Map<String, Property> properties;
    ...
    public getPropertyA() {
        return properties.get("propertyA");
    }
    ...
}

答案 1 :(得分:0)

您无法为外部表创建实体,然后将属性,属性和属性映射为此表的委托吗?

@Entity
public class AnEntity {

    @OneToOne(table = "properties")
    @JoinColumn("anentity_id")
    private Property property;

    public String getPropertyA() {
        return property.getValueA();
    }
}

注意:省略空检查! : - )