Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用InheritanceType.TABLE_PER_类时,eclipselink请求类指示符字段_Java_Inheritance_Jpa_Eclipselink_Table Per Class - Fatal编程技术网

Java 使用InheritanceType.TABLE_PER_类时,eclipselink请求类指示符字段

Java 使用InheritanceType.TABLE_PER_类时,eclipselink请求类指示符字段,java,inheritance,jpa,eclipselink,table-per-class,Java,Inheritance,Jpa,Eclipselink,Table Per Class,我已经花了几个小时试着做那份工作了。 我正在使用历史记录策略为我的一些表创建一个完整的历史记录。这是在抽象类中定义的。 然后我有一个普通实体实现这个类并定义它的字段。 然后我想使用一个从上层阶级继承的类,但是它的表被设置为历史表。这非常有效,唯一的问题是,当我查询历史化表(lasso_warehandling_条目)时,它总是从历史实体(lasso_warehandling_条目_hist)返回结果。所以我加了一行 descriptor.getInheritancePolicy().SetSho

我已经花了几个小时试着做那份工作了。 我正在使用历史记录策略为我的一些表创建一个完整的历史记录。这是在抽象类中定义的。 然后我有一个普通实体实现这个类并定义它的字段。 然后我想使用一个从上层阶级继承的类,但是它的表被设置为历史表。这非常有效,唯一的问题是,当我查询历史化表(lasso_warehandling_条目)时,它总是从历史实体(lasso_warehandling_条目_hist)返回结果。所以我加了一行 descriptor.getInheritancePolicy().SetShouldReadSubclass(false); 我在某个地方读到,它应该能解决我的问题。不幸的是,事实并非如此。 现在我总是收到以下信息:

Exception Description: The descriptor [RelationalDescriptor(dao.LassoWarehandlingEntry --> [DatabaseTable(lasso_warehandling_entry)])] has been set to use inheritance, but a class indicator field has not been defined. 
When using inheritance, a class indicator field or class extraction method must be set. 
Parent Descriptor: [RelationalDescriptor(org.rorotec.lasso.dao.LassoWarehandlingEntry --> [DatabaseTable(lasso_warehandling_entry)])]
Descriptor: RelationalDescriptor(dao.LassoWarehandlingEntry --> [DatabaseTable(lasso_warehandling_entry)])
因为我们为每个实体使用一个单独的表,所以使用指示符字段没有多大意义。不管怎么说,我就是没能把这条消息传出去。 有人知道我该怎么解决吗? 代码如下所示:

@MappedSuperclass
@Customizer(abstractDao.HistoryCustomizer.class)
public abstract class AbstractAuditedOzlEntity {
// defined the columns all the autited classes have
...
}

public class HistoryCustomizer implements DescriptorCustomizer {

    public void customize(ClassDescriptor descriptor) {
        HistoryPolicy policy = new HistoryPolicy();
        policy.addHistoryTableName(descriptor.getTableName()  + "_hist");
        policy.addStartFieldName("start_date");
        policy.addEndFieldName("end_date");
        descriptor.setHistoryPolicy(policy);

        // This here I added afterwards, as described in the text, and is the reason for the error message i get
        descriptor.getInheritancePolicy().setShouldReadSubclasses(false);
     }
}

@Entity
// when i define the inhertiancetype already in the abstract class, it doesn't seem to have any influence, so I added it here.
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
@Table(name = "lasso_warehandling_entry")
public class LassoWarehandlingEntry extends AbstractAuditedOzlEntity implements Serializable {
// define the specific stuff of this table
... 

}

@Entity
@Table(name = "lasso_warehandling_entry_hist")
@AttributeOverride(name="id", column=@Column(name="hist_id"))
public class LassoWarehandlingEntryHist extends LassoWarehandlingEntry {
...
// add the columns which only exist in the history tables like end_date
}

我和你面临同样的问题

错误消息表示EclipseLink无法区分您是在查询LassoWarehandlingEntry还是在查询LassoWarehandlingEntry。(必须设置类指示符字段或类提取方法。)因此,如果查询LassoWarehandlingEntry,EclipseLink将同时查询LassoWarehandlingEntry和LassoWarehandlingEntry。所以我猜你是想用

descriptor.getInheritancePolicy().setShouldReadSubclasses(false);
不幸的是,由于我们使用的是InheritanceType.TABLE_PER_类,因此似乎无法使用鉴别器注释(@DiscriminatorColumn和@DiscriminatorValue)。这两个注释仅适用于InheritanceType.SINGLE_表和InheritanceType.JOINED。类提取器也不工作。更多信息请访问

无论如何,我通过创建一个抽象类解决了这个问题,这个抽象类将由两个具体类继承。就你而言:


@MappedSuperclass
public abstract class AbstractLassoWarehandlingEntry extends AbstractAuditedOzlEntity implements Serializable {
// define the specific stuff of this table
... 

}


@Entity
@Table(name = "lasso_warehandling_entry")
public class LassoWarehandlingEntry extends AbstractLassoWarehandlingEntry implements Serializable {
// Empty class as the content is already defined in AbstractLassoWarehandlingEntry

}


@Entity
@Table(name = "lasso_warehandling_entry_hist")
@AttributeOverride(name="id", column=@Column(name="hist_id"))
public class LassoWarehandlingEntryHist extends AbstractLassoWarehandlingEntry {
...
// add the columns which only exist in the history tables like end_date
}

通过使用这种方法,您可以从HistoryCustomizer类中删除以下行


descriptor.getInheritancePolicy().setShouldReadSubclasses(false);
现在,如果查询LassoWarehandlingEntry,EclipseLink将只对LassoWarehandlingEntry表执行1个查询

希望这有帮助

这将有助于:

if (descriptor.hasInheritance()) {
    descriptor.getInheritancePolicy().setShouldReadSubclasses(false);
}
发件人:

使用此方法时必须小心,因为它会惰性地初始化继承策略

对不使用继承的描述符调用此函数将导致问题,必须始终首先调用hasInheritation()