有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

泛型JPA:java。lang.IllegalArgumentException在持久化映射时<Entity,Entity>

我有一个这样的抽象基类,并且希望持久化子类的映射myMap

public abstract class MyGenericAbstractClass<V, E> {

Map<E, SomeClass<V>> myMap;

public Map<E, SomeClass<V>> getMyMap() {
    return myMap;
}

由于我不能在抽象基类中注释泛型映射(因为JPA需要创建表的类型信息),所以我决定重写子类中的getter并进行属性访问

@Entity
@Access(AccessType.FIELD)
public class MyGenericClass extends MyGenericAbstractClass<VImpl, EImpl> {

@Id
@GeneratedValue
private Long id;

  @Access(AccessType.PROPERTY)
  @ManyToMany(cascade = CascadeType.PERSIST)
  @MapKeyJoinColumn(name = "EIMPL_ID")
  @JoinTable(name = "MapName")
  @Override
  public Map<EImpl, SomeClass<VImpl>> getMyMap() {
    return myMap;
  }

}

SomeClass.java现在看起来是这样的:

@Entity
public class SomeClass<V> {

@Id
@GeneratedValue
private Long id;

@OneToOne(targetEntity = jpa.test.minimalExample.VImpl.class, cascade = CascadeType.ALL)
V source;

@OneToOne(targetEntity = jpa.test.minimalExample.VImpl.class, cascade = CascadeType.ALL)
V target;

}

主要内容:

EntityManagerFactory factory =   Persistence.createEntityManagerFactory("Minimal");
EntityManager em = factory.createEntityManager();         
em.getTransaction().begin();

MyGenericClass mgc = new MyGenericClass();
SomeClass<VImpl> bar = new SomeClass<VImpl>();
EImpl foo = new EImpl();
Map<EImpl,SomeClass<VImpl>> mgcMap = new HashMap<EImpl,SomeClass<VImpl>>();    

mgcMap.put(foo, bar);
mgc.setMyMap(mgcMap);

em.persist(foo);
em.persist(mgc);        
em.getTransaction().commit();

EImplVImpl几乎都是空的实体(只有id)

坚持。xml:

 <?xml version="1.0" encoding="UTF-8" ?>
    <persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
      version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
      <persistence-unit name="Minimal" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>    

    <class>jpa.test.minimalExample.SomeClass</class>
    <class>jpa.test.minimalExample.MyGenericClass</class>
    <class>jpa.test.minimalExample.MyGenericAbstractClass</class>
    <class>jpa.test.minimalExample.VImpl</class>
    <class>jpa.test.minimalExample.EImpl</class>


 <properties>
  <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver" />
  <property name="javax.persistence.jdbc.url"  value="jdbc:derby://localhost:1527/simpleTest/;create=true" />
  <property name="javax.persistence.jdbc.user" value="test" />
  <property name="javax.persistence.jdbc.password" value="test" />

  <property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
  <property name="eclipselink.ddl-generation.output-mode" value="database" />
 </properties>

  </persistence-unit>
</persistence> 

最后,这是我得到的错误:

[EL Warning]: 2016-05-27 13:55:12.675--UnitOfWork(674882504)--Exception [EclipseLink-26] (Eclipse Persistence Services - 2.6.2.v20151217-774c696): org.eclipse.persistence.exceptions.DescriptorException Exception Description: Trying to get value for instance variable [id] of type [java.lang.Long] from the object [jpa.test.minimalExample.SomeClass]. The specified object is not an instance of the class or interface declaring the underlying field. Internal Exception: java.lang.IllegalArgumentException: Can not set java.lang.Long field jpa.test.minimalExample.VImpl.id to jpa.test.minimalExample.SomeClass Mapping: org.eclipse.persistence.mappings.DirectToFieldMapping[id-->VIMPL.ID] Descriptor: RelationalDescriptor(jpa.test.minimalExample.VImpl --> [DatabaseTable(VIMPL)])

对于如何修复注释/修改我的类结构以使像这样的映射持久化成为可能的任何建议,我们都将不胜感激。我也非常想了解错误本身(以及它为什么会出现),而不是仅仅得到一个解决方法,尽管现在我会得到任何我能得到的帮助

先谢谢你


共 (0) 个答案