我最近对一年左右运行良好的JPA应用程序进行了一些更改。我添加了几个新表,我遇到了一些问题。我无法发布我的架构图像,因为我没有10个帖子,但我有一个项目表和一个产品表。许多产品可以具有相同的项目,因此产品表具有item_id
以下是我的商品实体
@Entity
@Table(name="item")
public class Item implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private Integer _id;
private Double carbonValue;
private String description;
private String name;
private Integer discount;
@OneToMany(mappedBy="item")
private Collection<Product> products;
// getters and setters etc
这是我的产品实体
@Entity
@Table(name="product")
public class Product implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private Integer _id;
private Integer deleted;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="ITEM_ID", referencedColumnName="_ID")
private Item item;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="SUPPLIER_ID", referencedColumnName="_ID")
private Supplier supplier;
@OneToMany(mappedBy = "product")
private Collection<Consumption> consumptions;
我正在使用eclipselink 2.2
当我在项目表上运行查询时如下:
public final static String SELECT_ALL_ENTITIES_SQL = "SELECT o FROM Item AS o";
public List<Item> getAllEntities() {
final EntityManager entityManager = DaoUtils.getEntityManagerFactory().createEntityManager();
final List<Item> items;
try {
items = (List<Item>) entityManager.createQuery(SELECT_ALL_ENTITIES_SQL).getResultList();
} finally {
entityManager.close();
}
return items;
}
我收到以下错误:
[EL Info]: 2012-10-24 14:16:23.798--ServerSession(1351669994)--EclipseLink, version: Eclipse Persistence Services - 2.2.0.v20110202-r8913
[EL Severe]: 2012-10-24 14:16:24.16--ServerSession(1351669994)--Local Exception Stack:
Exception [EclipseLink-0] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.IntegrityException
Descriptor Exceptions:
---------------------------------------------------------
Exception [EclipseLink-93] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: The table [item] is not present in this descriptor.
Descriptor: RelationalDescriptor(database.entities.Product --> [DatabaseTable(product)])
Exception [EclipseLink-93] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: The table [circle] is not present in this descriptor.
Descriptor: RelationalDescriptor(database.entities.Refund --> [DatabaseTable(refund)])
Runtime Exceptions:
---------------------------------------------------------
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:476)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:407)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.postConnectDatasource(DatabaseSessionImpl.java:680)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:628)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:233)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:394)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getServerSession(EntityManagerFactoryImpl.java:185)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:242)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:230)
at database.dao.ItemDao.getAllEntities(ItemDao.java:24)
at database.dao.ItemDao.main(ItemDao.java:19)
Descriptor Exceptions:
---------------------------------------------------------
Local Exception Stack:
Exception [EclipseLink-93] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: The table [item] is not present in this descriptor.
Descriptor: RelationalDescriptor(database.entities.Product --> [DatabaseTable(product)])
at org.eclipse.persistence.exceptions.DescriptorException.tableNotPresent(DescriptorException.java:1628)
at org.eclipse.persistence.descriptors.ClassDescriptor.getTable(ClassDescriptor.java:2478)
at org.eclipse.persistence.descriptors.ClassDescriptor.buildField(ClassDescriptor.java:760)
at org.eclipse.persistence.descriptors.ClassDescriptor.buildField(ClassDescriptor.java:749)
at org.eclipse.persistence.mappings.OneToManyMapping.initializeTargetForeignKeysToSourceKeys(OneToManyMapping.java:796)
at org.eclipse.persistence.mappings.OneToManyMapping.initializeReferenceDescriptor(OneToManyMapping.java:726)
at org.eclipse.persistence.mappings.ForeignReferenceMapping.initialize(ForeignReferenceMapping.java:1160)
at org.eclipse.persistence.mappings.CollectionMapping.initialize(CollectionMapping.java:1095)
at org.eclipse.persistence.mappings.OneToManyMapping.initialize(OneToManyMapping.java:466)
at org.eclipse.persistence.descriptors.ClassDescriptor.initialize(ClassDescriptor.java:2744)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:453)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:407)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.postConnectDatasource(DatabaseSessionImpl.java:680)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:628)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:233)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:394)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getServerSession(EntityManagerFactoryImpl.java:185)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:242)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:230)
at database.dao.ItemDao.getAllEntities(ItemDao.java:24)
at database.dao.ItemDao.main(ItemDao.java:19)
我在这里做错了什么?
更新:在数据库中,我删除了产品和商品之间的关系。在我的代码中,我还删除了两个实体之间的任何引用,所以现在数据库表和实体根本没有任何关系。我仍然得到同样的错误?????
解决:当我在另一个不相关的实体中对我的实体进行编码时,我非常认真地将一个项目误导了一个产品。我现在正在敲桌子。
答案 0 :(得分:0)
请勿使用@PrimaryKeyJoinColumn
。通常情况下,使用JPA Inheritance
。使用@JoinColumn annotation
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="ITEM_ID", referencedColumnName="ID")
private Item item;
Supplier
也是如此。
更新:这是EclipseLink
的问题。 Bug 282571
尝试修复如下:
@Entity
@Table(name="product")
@SecondaryTable(name="item", pkJoinColumns=@PrimaryKeyJoinColumn(name="_ID"))
public class Product implements Serializable {
}
根据您的错误日志:
Exception Description: The table [circle] is not present in this descriptor.
Descriptor: RelationalDescriptor(database.entities.Refund --> [DatabaseTable(refund)])
您可能需要在SecondaryTable
实体Refund
中使用circle
注释。
答案 1 :(得分:0)
如果Item
不需要明确了解其所属的产品,则不需要mappedBy
。 @JoinColumn
表中的Product
拥有这种关系,在这里放置项目的明确知识是很有意义的。
请执行以下操作:
编辑行
@OneToMany(mappedBy="item")
private Collection<Product> products;
向任一方向
@OneToMany
private Collection<Product> products;
还是更好
@OneToMany(fetch = FetchType.LAZY)
private Collection<Product> products;