3

我最近对一个运行良好一年左右的 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)

我在这里做错了什么?

更新:在数据库上,我删除了产品和项目之间的关系。在我的代码中,我还删除了两个实体之间的任何引用,因此现在数据库表和实体之间根本没有任何关系。我仍然得到同样的错误??????

已解决:当我在另一个不相关的实体中编码我的实体时,我不小心将一个项目贴错了产品标签。我现在正用头撞桌子。

4

2 回答 2

1

不要使用@PrimaryKeyJoinColumn. 通常,它是使用JPA Inheritance. 利用@JoinColumn annotation

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="ITEM_ID", referencedColumnName="ID")
private Item item;

对于Supplier.

更新:这是一个问题EclipseLink错误 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注释。Refundcircle

于 2012-10-24T04:09:50.487 回答
1

如果Item不需要明确了解它所属的产品,则mappedBy不需要。表@JoinColumn中的Product拥有这种关系,将项目的表达知识放在此处是有道理的。

因此,请执行以下操作:

编辑行

@OneToMany(mappedBy="item")
private Collection<Product> products;

要么

@OneToMany
private Collection<Product> products;

或者更好

@OneToMany(fetch = FetchType.LAZY)
private Collection<Product> products;
于 2018-06-28T13:38:28.590 回答