使用 hibernate 从数据库中检索一组原始类型

发布于 2025-01-04 16:43:24 字数 1053 浏览 2 评论 0原文

我使用的是 informix 数据库,并且有两个表;实例及联系方式。联系人表有以下字段; contact_id、fname 和 lname。实例表具有以下字段,instance_id、name 和 contact_ids(contact_ids 是 contact ids 的 informix set 集合,com.informix.jdbc.IfxCollection@429681e8)。 我使用hibernate进行数据持久化。我的实例类的代码如下所示:

@Entity

public class Instance{

@Id
private int instance_id;

private String name;

@Lob
private Set<Integer> contact_ids
     ....
     setters and getters

}

联系人类:

@Entity

public class Contact{

@Id
private int contact_id;

private String fname;

private String lname;

     ....

     setters and getters
}

当我加载实例实体时,出现以下错误:

20:32:18,527 ERROR [jsp:154] java.sql.SQLException: **Can't convert to: binary stream**
    at com.informix.util.IfxErrMsg.getSQLMinorException(IfxErrMsg.java:575)
    at com.informix.jdbc.IfxObject.toBlob(IfxObject.java:647)
    at com.informix.jdbc.IfxResultSet.getBlob(IfxResultSet.java:3338)
    at com.informix.jdbc.IfxResultSet.getBlob(IfxResultSet.java:3437)

我只想检索该集合。

I am using an informix database and I have two tables; instance and contact. Contact table has the following fields; contact_id, fname and lname. Instance table has the following fields, instance_id, name and contact_ids(contact_ids is an informix set collection of contact ids, com.informix.jdbc.IfxCollection@429681e8).
I use hibernate for data persistence. The code for my Instance Class looks like this:

@Entity

public class Instance{

@Id
private int instance_id;

private String name;

@Lob
private Set<Integer> contact_ids
     ....
     setters and getters

}

Contact Class:

@Entity

public class Contact{

@Id
private int contact_id;

private String fname;

private String lname;

     ....

     setters and getters
}

When I load an Instance Entity I get the following error:

20:32:18,527 ERROR [jsp:154] java.sql.SQLException: **Can't convert to: binary stream**
    at com.informix.util.IfxErrMsg.getSQLMinorException(IfxErrMsg.java:575)
    at com.informix.jdbc.IfxObject.toBlob(IfxObject.java:647)
    at com.informix.jdbc.IfxResultSet.getBlob(IfxResultSet.java:3338)
    at com.informix.jdbc.IfxResultSet.getBlob(IfxResultSet.java:3437)

I simply want to retrieve the set.

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

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

发布评论

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

评论(2

白日梦 2025-01-11 16:43:24

尝试使用 @ElementCollection 注释,如下所示:

   @ElementCollection
   @CollectionTable(name="contact_ids", joinColumns=@JoinColumn(name="instance_id"))
   @Column(name="contact_id")
   public Set<Integer> contactIds;

但是,对于您的场景,我建议实际实体本身之间存在 OneToMany 关系,因此在您的实例类中,您将拥有:而不是 contactIds 字段:

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "instance", cascade = {...})
    public Set<Contact> contacts;

并且在您的 Contact 类中,您还可以有(与上面匹配):

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "instance_id", referencedColumnName = "instance_id", nullable = false)
public Instance instance;

Try using the @ElementCollection annotation as follows:

   @ElementCollection
   @CollectionTable(name="contact_ids", joinColumns=@JoinColumn(name="instance_id"))
   @Column(name="contact_id")
   public Set<Integer> contactIds;

However, for your scenario, I would recommend a OneToMany relationship between the actual entities themselves, so in your instance class, instead of the contactIds field, you'd have:

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "instance", cascade = {...})
    public Set<Contact> contacts;

and in your Contact class you also could have (to match the above):

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "instance_id", referencedColumnName = "instance_id", nullable = false)
public Instance instance;
冬天旳寂寞 2025-01-11 16:43:24

您需要自定义您的 Informix typeHandeler:

假设您正在使用 myBatis 添加:

<typeHandlers>
    <typeHandler javaType="string" jdbcType="BLOB" handler="org.apache.ibatis.type.StringTypeHandler"/>
</typeHandlers>

在您的 mybatis-config.xml 文件

提示:“javaType”应该是您想要的类型

You need to customize Your Informix typeHandeler:

assuming You are using myBatis add:

<typeHandlers>
    <typeHandler javaType="string" jdbcType="BLOB" handler="org.apache.ibatis.type.StringTypeHandler"/>
</typeHandlers>

at Your mybatis-config.xml file

hint: "javaType" should be Your desired type

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