在openJpa 2中没有joinTable的单向@oneToMany

时间:2014-11-19 09:59:23

标签: openjpa

我正在尝试在 openJpa 2.3.0 中创建单向OneToMany映射,但我还想要定义将在源表上保存foreighn键的列名。据我所知,在JPA 2.0中,可以按如下方式完成:

@Entity
public class Source {


    private List<Target> targets = new ArrayList<>();


    @OneToMany
    @JoinColumn(name="SOURCE_FK")
    public List<Target> getTargets() {
        return targets;
    }
}

但我得到以下例外:

 <openjpa-2.3.0-r422266:1540826 fatal user error> org.apache.openjpa.persistence.ArgumentException: You have supplied columns for "Source.targets", but this mapping cannot have columns in this context.
at org.apache.openjpa.jdbc.meta.MappingInfo.assertNoSchemaComponents(MappingInfo.java:382)
at org.apache.openjpa.jdbc.meta.strats.RelationToManyTableFieldStrategy.map(RelationToManyTableFieldStrategy.java:97)
at org.apache.openjpa.jdbc.meta.strats.RelationCollectionTableFieldStrategy.map(RelationCollectionTableFieldStrategy.java:94)
at org.apache.openjpa.jdbc.meta.FieldMapping.setStrategy(FieldMapping.java:146)
at org.apache.openjpa.jdbc.meta.RuntimeStrategyInstaller.installStrategy(RuntimeStrategyInstaller.java:82)
at org.apache.openjpa.jdbc.meta.FieldMapping.resolveMapping(FieldMapping.java:496)
at org.apache.openjpa.jdbc.meta.FieldMapping.resolve(FieldMapping.java:461)
at org.apache.openjpa.jdbc.meta.ClassMapping.resolveMapping(ClassMapping.java:854)
at org.apache.openjpa.meta.ClassMetaData.resolve(ClassMetaData.java:1811)
at org.apache.openjpa.meta.MetaDataRepository.processBuffer(MetaDataRepository.java:829)
at org.apache.openjpa.meta.MetaDataRepository.resolveMapping(MetaDataRepository.java:784)
at org.apache.openjpa.meta.MetaDataRepository.resolve(MetaDataRepository.java:664)
at org.apache.openjpa.meta.MetaDataRepository.getMetaDataInternal(MetaDataRepository.java:418)
at org.apache.openjpa.meta.MetaDataRepository.getMetaData(MetaDataRepository.java:389)
at org.apache.openjpa.kernel.BrokerImpl.persistInternal(BrokerImpl.java:2666)
at org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2604)
at org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2587)
at org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2491)
at org.apache.openjpa.kernel.DelegatingBroker.persist(DelegatingBroker.java:1077)
at org.apache.openjpa.persistence.EntityManagerImpl.persist(EntityManagerImpl.java:716)
at 

似乎openJpa 2.3.0不支持单向oneToMany中的Joincolumn。这是真的还是我错过了什么?如果是这种情况,是否有不使用连接表的解决方法?

1 个答案:

答案 0 :(得分:3)

回答我自己的问题:  OpenJpa 2在OneToMany关系中支持@JoinColumn。

正如我发现here,有各种原因导致此异常。

在我的例子中,问题是persistence.xml中的错误模式URL

有问题的一行是以下一行:

 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">

这告诉OpenJpa遵守JPA 1.0规范(JPA 1.0不支持OneToMany关系中的此注释,但JPA 2确实如此) 我将版本更改为2.0并且工作正常。

xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0">