JPA鉴别器列问题

发布于 2024-10-12 13:48:16 字数 2574 浏览 8 评论 0原文

大家好。 我有这组类:

@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_TIORAPPORTO = '1 ' 一个,甚至 t0.COD_TIORAPPORTO = '1'。

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

现在,我与超类 Rapporto 建立多对多关联,如下所述 此处

如果我保留 @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")删除空间一切正常,但我没有收到任何数据(即使数据存在于数据库中)。

我不知道该怎么办...... 有什么想法吗?

亲切的问候

马西莫

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉城凉梦凉人心 2024-10-19 13:48:16

使用绑定时,您的数据库似乎存在字符与空格比较的问题。你使用什么数据库?

有几种可能的解决方案,首先我会检查您的 JDBC 驱动程序,看看它是否有任何修复或选项来解决此问题。

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

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

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

如果您可以更改数据,那么我建议只需将任何“1”更新为“01”,然后您就可以使用“01”作为指示符。

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.

古镇旧梦 2024-10-19 13:48:16

数据库中的值似乎包含尾随空格。我建议对表运行查询并删除尾随空格。然后让鉴别器值为 "1" (无空格)。

事实上,如果您无论如何都要使用数字,则应该使用 DiscriminatorType.INTEGER

It appears that the values in your database contain a trailing space. I'd suggest to run a query against the table and remove the trailing whitespace. Then have the discriminator value be "1" (no space)

In fact, if you are going to use numbers anyway, you should use DiscriminatorType.INTEGER

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文