本文介绍了JPA鉴别器列问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!我有这套课程:

@Entity
@Table(name = "S_MC_CC_RAPPORTI")
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="COD_TIPORAPPORTO",
                     discriminatorType=DiscriminatorType.CHAR, length=1)
public abstract class Rapporto implements Serializable {

  private static final long serialVersionUID = -5567166522882040440L;

  @Id
  @Column(name = "COD_RAPPORTO")
  protected Long codiceRapporto;

这个小主题:

@Entity
@Table(name="S_MC_CC_CCCLIENTI")
@DiscriminatorValue("1 ")
public class ContoCorrente extends Rapporto {
  private static final long serialVersionUID = -3380622649760983262L;

  @Column(name = "DESC_DIVISA")
  private String divisa;

问题出在鉴别值上:在表上,该列为CHAR(2).

当我在子类上使getItemById都正常工作时:这是生成的eclipselink sql :(我将列定义替换为*,以便于阅读)

[EL Fine]: 2011-01-16 16:01:14.531--ServerSession(5230193)--Connection(11601738)--Thread(Thread[main,5,main])--  SELECT * FROM S_MC_CC_RAPPORTI t0, S_MC_CC_CCCLIENTI t1 WHERE ((t0.COD_RAPPORTO = ?) AND ((t1.COD_RAPPORTO = t0.COD_RAPPORTO) AND (t0.COD_TIPORAPPORTO = ?)))
 bind => [1120676, 1 ]

如果我更改@DiscriminatorValue("1")删除空间,则找不到数据:这是相对生成的sql

[EL Fine]: 2011-01-16 16:03:46.671--ServerSession(5230193)--Connection(11601738)--Thread(Thread[main,5,main])--SELECT * FROM S_MC_CC_RAPPORTI t0, S_MC_CC_CCCLIENTI t1 WHERE ((t0.COD_RAPPORTO = ?) AND ((t1.COD_RAPPORTO = t0.COD_RAPPORTO) AND (t0.COD_TIPORAPPORTO = ?)))
 bind => [1120676, 1]

请注意,这两个查询在TOAD上都能正常工作:t0.COD_TIPORAPPORTO ='1',甚至t0.COD_TIPORAPPORTO ='1'.

好:所以我认为@DiscriminatorValue("1 ")是正确的.

现在,我按照所述

如果我拥有@DiscriminatorValue("1 "),我会得到

[EL Warning]: 2011-01-16 16:09:33.421--ServerSession(29839159)--Thread(Thread[main,5,main])--Exception [EclipseLink-43] (Eclipse Persistence Services - 2.1.2.v20101206-r8635): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Missing class for indicator field value [1] of type [class java.lang.String].
Descriptor: RelationalDescriptor(it.alten.intesasanpaolo.contratto.domain.core.rapporto.Rapporto --> [DatabaseTable(S_MC_CC_RAPPORTI)])

如果我更改@DiscriminatorValue("1"),则删除所有空间都可以,但是我没有收到任何数据(即使DB上存在数据).

我不知道该怎么办....有什么主意吗?

亲切的问候

马西莫(Massimo)

解决方案

在使用绑定时,您的数据库似乎存在char与空格比较的问题.您正在使用什么数据库?

有几种可能的解决方案,首先,我将与您的JDBC驱动程序进行检查,以查看它是否具有解决此问题的修补程序或选项.

一种选择是在您的persistence.xml中设置"eclipselink.jdbc.bind-parameters" ="false",这应该可以解决该问题.

另一种解决方案是在EclipseLink中禁用字符修整(这个似乎没有暴露于JPA持久性属性(请记录一个错误),因此您需要使用SessionCustomizer进行设置)session.getLogin().setShouldTrimStrings(false);

一个更复杂的解决方案是使用DescriptorCustomizer在描述符上设置您自己的ClassExtractor,以使用您自己的代码确定类.

如果您可以更改数据,那么我建议您仅将任何'1'更新为'01',然后就可以使用'01'作为指标.

Hallo all.I have this set of classes:

@Entity
@Table(name = "S_MC_CC_RAPPORTI")
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="COD_TIPORAPPORTO",
                     discriminatorType=DiscriminatorType.CHAR, length=1)
public abstract class Rapporto implements Serializable {

  private static final long serialVersionUID = -5567166522882040440L;

  @Id
  @Column(name = "COD_RAPPORTO")
  protected Long codiceRapporto;

And this sublass:

@Entity
@Table(name="S_MC_CC_CCCLIENTI")
@DiscriminatorValue("1 ")
public class ContoCorrente extends Rapporto {
  private static final long serialVersionUID = -3380622649760983262L;

  @Column(name = "DESC_DIVISA")
  private String divisa;

The problem is on the discriminator value: on the table the column is CHAR(2).

When I make the getItemById on the subclass all works fine: this is the generated eclipselink sql: (I replaced the column definition with * for easy to read)

[EL Fine]: 2011-01-16 16:01:14.531--ServerSession(5230193)--Connection(11601738)--Thread(Thread[main,5,main])--  SELECT * FROM S_MC_CC_RAPPORTI t0, S_MC_CC_CCCLIENTI t1 WHERE ((t0.COD_RAPPORTO = ?) AND ((t1.COD_RAPPORTO = t0.COD_RAPPORTO) AND (t0.COD_TIPORAPPORTO = ?)))
 bind => [1120676, 1 ]

If I change the @DiscriminatorValue("1") removing the space the data is not found: this is the relative generated sql

[EL Fine]: 2011-01-16 16:03:46.671--ServerSession(5230193)--Connection(11601738)--Thread(Thread[main,5,main])--SELECT * FROM S_MC_CC_RAPPORTI t0, S_MC_CC_CCCLIENTI t1 WHERE ((t0.COD_RAPPORTO = ?) AND ((t1.COD_RAPPORTO = t0.COD_RAPPORTO) AND (t0.COD_TIPORAPPORTO = ?)))
 bind => [1120676, 1]

Note that the two queries work fine with TOAD: both the t0.COD_TIPORAPPORTO = '1 ' one, even the t0.COD_TIPORAPPORTO = '1'.

OK: so I think that the @DiscriminatorValue("1 ") is correct.

Now I make an association many-to-many with the super class Rapporto as described here

If I mantain the @DiscriminatorValue("1 ") I get

[EL Warning]: 2011-01-16 16:09:33.421--ServerSession(29839159)--Thread(Thread[main,5,main])--Exception [EclipseLink-43] (Eclipse Persistence Services - 2.1.2.v20101206-r8635): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Missing class for indicator field value [1] of type [class java.lang.String].
Descriptor: RelationalDescriptor(it.alten.intesasanpaolo.contratto.domain.core.rapporto.Rapporto --> [DatabaseTable(S_MC_CC_RAPPORTI)])

If I change the @DiscriminatorValue("1") removing the space all work fine but I receive no data (even If the data is present on DB).

I dunno what to do....Any idea?

Kind regards

Massimo

解决方案

Your database seems to have an issue with char comparison with spaces when using binding. What database are you using?

There are several possible solutions, first I would check with your JDBC driver to see if it has any fixes or options to solve this.

One option is to set "eclipselink.jdbc.bind-parameters"="false" in your persistence.xml, this should resolve the issue.

Another solution is to disable char trimming in EclipseLink, (this one does not seem to be exposed to JPA persistence properties (please log a bug), so you need to use a SessionCustomizer to set it)session.getLogin().setShouldTrimStrings(false);

A more involved solution is to use a DescriptorCustomizer to set your own ClassExtractor on the descriptor to use you own code to determine the class.

If you can change the data, then I would recommend just updating any '1 ' to '01' then you can just use '01' as the indicator.

这篇关于JPA鉴别器列问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 01:53