映射不会检索任何值

时间:2013-12-03 04:29:07

标签: java jpa

我有一个客户端对象,它基于DescripterCustomizer实现(在AddressFilter中)映射到住宅和邮政地址。但是,当我通过id检索客户端时,它不会填充地址。有人可以帮忙吗?我的课程如下:

@Entity
@Table(name = "cli_clients")
@Customizer(AddressFilter.class)
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="client_type", discriminatorType=DiscriminatorType.INTEGER)
@MappedSuperclass
public abstract class Client implements Serializable {
@EmbeddedId
protected ClientPK id;

@OneToOne(mappedBy="client", fetch = FetchType.EAGER)
@JoinColumn(name = "c_roid", referencedColumnName = "c_roid")
private Address currentResidentialAddress;

@OneToOne(mappedBy="client", fetch = FetchType.EAGER)
@JoinColumn(name = "c_roid", referencedColumnName = "c_roid")
private Address currentPostalAddress;
...
}

@Entity
@Table(name = "cli_addresses")
public class Address implements Serializable{
@EmbeddedId
private AddressPK id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "c_roid", referencedColumnName = "c_roid")
private Client client;
...
}

public class AddressFilter implements DescriptorCustomizer{

@Override
public void customize(ClassDescriptor descriptor) throws Exception {
    defineCurrentResidentialAddressCriteria(descriptor);
    defineCurrentPostalAddressCriteria(descriptor);
}

private void defineCurrentResidentialAddressCriteria(ClassDescriptor descriptor){
    OneToOneMapping mapping = (OneToOneMapping) descriptor.getMappingForAttributeName("currentResidentialAddress");
    if (mapping == null) {
        throw new IllegalArgumentException("Cannot find mapping for address.currentResidentialAddress");
    }

    Expression baseExp = mapping.buildSelectionCriteria();
    ExpressionBuilder eb = baseExp.getBuilder();
    Expression addressTypeExpression = eb.get("addressType.code").equal(AddressType.RESIDENTIAL);
    Expression endDateExpression = eb.get("toDateTime").greaterThan(eb.currentDate()).or(eb.get("toDateTime").isNull());

mapping.setSelectionCriteria(baseExp.and(addressTypeExpression).and(endDateExpression));
}

private void defineCurrentPostalAddressCriteria(ClassDescriptor descriptor){
    OneToOneMapping mapping = (OneToOneMapping) descriptor.getMappingForAttributeName("currentPostalAddress");
    if (mapping == null) {
        throw new IllegalArgumentException("Cannot find mapping for address.currentPostalAddress");
    }

    Expression baseExp = mapping.buildSelectionCriteria();
    ExpressionBuilder eb = baseExp.getBuilder();
    Expression addressTypeExpression = eb.get("addressType.code").equal(AddressType.POSTAL);
    Expression endDateExpression = eb.get("toDateTime").greaterThan(eb.currentDate()).or(eb.get("toDateTime").isNull());

    mapping.setSelectionCriteria(baseExp.and(addressTypeExpression).and(endDateExpression));
}
}

0 个答案:

没有答案