Hibernate返回带有空值的列表(带有List类型的OneToMany注释) [英] Hibernate returns list with null values (OneToMany annotation with List type)

查看:379
本文介绍了Hibernate返回带有空值的列表(带有List类型的OneToMany注释)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,Hibernate(4.1.8.FINAL)返回一个带NULL值的列表(单向OneToMany映射)。



'获得:
我得到一个大小为21的List,其中EntryAddress位于第10个索引,第2个入口地址位于第20位索引。

 条目[地址= [null,null,null,null,null,null,null,null,null,EntryAddress [id = 5,entryId = 3,precedence = 10 ,line = Line 3.1],null,null,null,null,null,null,null,EntryAddress [id = 6,entryId = 3,precedence = 20,line = Line 3.2]]] 

我期望的 - 我期望List只有两个EntryAddress对象:

条目地址= [EntryAddress [id = 5,entryId = 3,precedence = 10,line = Line 3.1],EntryAddress [id = 6 ,entryId = 3,precedence = 20,line = Line 3.2]]]

这里是最小的源代码:

  @Entity 
@Table(name =entry)
public class Entry {
...
@OneToMany(fetch = FetchType。 EAGER,orphanRemoval = true)
@JoinColumn(name =entry_id)
@OrderColumn(name =precedence)
private List< EntryAddress>地址;
...
}



  @Entity 
@Table(name =entry_address)
公共类EntryAddress {
@Id
@GeneratedValue
@Column(name =id)
私人整数ID;

@Column(name =entry_id)
private Integer entryId;

@Column(name =precedence)
私有整数优先;
...
}

以下是mysql结构(引擎InnoDB):

  CREATE TABLE条目(
id int(10)unsigned NOT NULL AUTO_INCREMENT,
name varchar(500 )NOT NULL,
active int(1)NOT NULL DEFAULT'0',
modifiedTS timestamp NOT NULL DEFAULT'0000-00-00 00:00:00'ON UPDATE CURRENT_TIMESTAMP,
createdTS timestamp NOT NULL DEFAULT'0000-00-00 00:00:00',
PRIMARY KEY(id)
)ENGINE = InnoDB DEFAULT CHARSET = utf8;

- ---------------------------------------- ----------------

CREATE TABLE entry_address(
id int(10)unsigned NOT NULL AUTO_INCREMENT,
entry_id int(10 )unsigned NULL,
precedence int(2)NULL DEFAULT'0',
line varchar(255)DEFAULT NULL,
PRIMARY KEY(id),
UNIQUE KEY entry_address_uq(entry_id ,优先)
)ENGINE = InnoDB DEFAULT CHARSET = utf8;

当我尝试用Set取代List时,集合没有NULL值,但顺序/优先顺序不起作用。



我发现的一个有趣点是,如果我将第一个EntryAddress的优先级设置为1,并将第二个EntryAddress的优先级设置为2,那么我得到一个大小为2.所以看起来优先级在List返回时起作用,但优先级只应用于排序。



你能告诉我我做错了什么吗? ?谢谢: - )

解决方案

我找到了解决方案,我用它解决了这个问题

  @OneToMany(orphanRemoval = true)
@LazyCollection(LazyCollectionOption.FALSE)
@JoinColumn(name =entry_id)
@OrderBy(优先)
私人列表< EntryAddress>地址;

@OneToMany(orphanRemoval = true)
@LazyCollection(LazyCollectionOption.FALSE)
@JoinColumn(name =entry_id)
@OrderBy(precedence)
私人列表< EntryContact>联系人;


I am experiencing a problem where as Hibernate (4.1.8.FINAL) returns a list with NULL values (unidirectional OneToMany mapping).

What I'm getting: I am getting a List with size of 21 where as EntryAddress is on 10th index, and 2nd Entry Address is on 20th index.

Entry [addresses=[null, null, null, null, null, null, null, null, null, null, EntryAddress [id=5, entryId=3, precedence=10, line=Line 3.1], null, null, null, null, null, null, null, null, null, EntryAddress [id=6, entryId=3, precedence=20, line=Line 3.2]]]

What I expect - I expect a List with only two EntryAddress objects:

Entry [addresses=[EntryAddress [id=5, entryId=3, precedence=10, line=Line 3.1], EntryAddress [id=6, entryId=3, precedence=20, line=Line 3.2]]]

Here's the minimal source code:

@Entity
@Table(name = "entry")
public class Entry {
    ...
    @OneToMany(fetch = FetchType.EAGER, orphanRemoval = true)
    @JoinColumn(name = "entry_id")
    @OrderColumn(name = "precedence")
    private List<EntryAddress> addresses;
    ...
}

@Entity
@Table(name = "entry_address")
public class EntryAddress {
    @Id
    @GeneratedValue
    @Column(name = "id")
    private Integer id;

    @Column(name = "entry_id")
    private Integer entryId;

    @Column(name = "precedence")
    private Integer precedence;
...
}

Here's the mysql structure (engine InnoDB):

CREATE TABLE  entry  (
   id  int(10) unsigned NOT NULL AUTO_INCREMENT,
   name  varchar(500) NOT NULL,
   active  int(1) NOT NULL DEFAULT '0',
   modifiedTS  timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
   createdTS  timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY ( id )
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

-- --------------------------------------------------------

CREATE TABLE  entry_address  (
   id  int(10) unsigned NOT NULL AUTO_INCREMENT,
   entry_id  int(10) unsigned NULL,
   precedence  int(2) NULL DEFAULT '0',
   line  varchar(255) DEFAULT NULL,
  PRIMARY KEY ( id ),
  UNIQUE KEY  entry_address_uq  ( entry_id , precedence )
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

When I try to replace "List" with "Set" then collection doesn't have NULL values, but the sequence/precedence is not working.

One interesting point I found is, if I set precedence of 1st EntryAddress to 1, and set precedence of 2nd EntryAddress to 2 then I am getting a List with a size of 2. So it seems the precedence plays a role during returning of List although the precedence should only be used for sorting.

Can you please tell me what I'm doing wrong? Thank you :-)

解决方案

I found the solution, I used it and this solved the issue

@OneToMany(orphanRemoval = true)
@LazyCollection(LazyCollectionOption.FALSE)
@JoinColumn(name = "entry_id")
@OrderBy("precedence")
private List<EntryAddress> addresses;

@OneToMany(orphanRemoval = true)
@LazyCollection(LazyCollectionOption.FALSE)
@JoinColumn(name = "entry_id")
@OrderBy("precedence")
private List<EntryContact> contacts;

这篇关于Hibernate返回带有空值的列表(带有List类型的OneToMany注释)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆