简体   繁体   English

休眠/ JPA的自定义选择

[英]Custom Select for hibernate/JPA

I'm using JPA over Hibernate, with an annotation configuration. 我在基于Hibernate的JPA上使用了批注配置。

I'm looking for doing a custom "select", that would allow me to know the size of a "lazy collection", without getting the collection. 我正在寻找做一个自定义的“选择”,这将使我知道“惰性集合”的大小,而无需获取集合。

I tried this, without success: 我尝试了这个,但没有成功:

@Entity
@Table(name="t_test")   
public class Test {

@Id
@Column(name="name", length=50)
private String name;

@Column(name="first_name", length=50)
private String firstName;

@ElementCollection(fetch=FetchType.LAZY)
@JoinTable(name = "tj_test_nicknames", schema="",
    joinColumns = {@JoinColumn(name = "name")} 
)
@ForeignKey(name = "FK_test_nickname")
@Column(name="nickname", nullable=false)
private Set<String> nickNames = new HashSet();

@Formula("(SELECT COUNT(t.nickname) FROM tj_test_nicknames t WHERE t.name = name)")
private int counter;

In my test, i'm just trying to display the counter number ( with a classic for ) 在我的测试中,我只是想显示计数器编号(带有的经典值)

Test test = new Test();
test.setFirstName("prenom");
test.setName("nom" + Math.random());

Set<String> nickNames = new HashSet<String>();
nickNames.add("surnom");
nickNames.add("toto");
test.setNickNames(nickNames);

testDAO.save(test);

Collection<Test> tests = testDAO.getAll();
if(tests!=null){
    for (Test testT : tests) {
        logger.info("Number of nicknames: " + testT.getNumberOfNicknames());
        logger.info("Size of the list:: " + testT.getNickNames().size());
    }
}

But i display 0 for each object instance ( that has instanciated lazy collection ). 但是我为每个对象实例显示0(实例化了lazy collection)。

org.hibernate.pretty.Printer listing entities:... org.hibernate.pretty.Printer列出实体:...

Test{counter=2, name=nom0.46579385535806883, firstName=prenom, nickNames=} 测试{counter = 2,name = nom0.46579385535806883,firstName = prenom,nickNames =}

Test{counter=0, name=nom0.6489676574372193, firstName=prenom, nickNames=[toto, surnom]} 测试{counter = 0,name = nom0.6489676574372193,firstName = prenom,nickNames = [toto,surnom]}

Test{counter=2, name=nom0.6362777689140984, firstName=prenom, nickNames=} 测试{counter = 2,name = nom0.6362777689140984,firstName = prenom,nickNames =}

Test{counter=2, name=nom0.15039481770844532, firstName=prenom, nickNames=} 测试{counter = 2,name = nom0.15039481770844532,firstName = prenom,nickNames =}

Test{counter=2, name=nom0.5580054662200352, firstName=prenom, nickNames=} 测试{counter = 2,name = nom0.5580054662200352,firstName = prenom,nickNames =}

EDIT: So i've understood that it's when the lazy collection nickNames is instanciated that it returns 0 and not the good number. 编辑:所以我知道这是当懒惰的集合nickNames被实例化时,它返回0而不是好的数字。

So it's really annoying, as i want to get the size in order not to intanciate the collection, but in the case i have the collection instanciated i must have a good information... 所以这真的很烦人,因为我想获取大小以免使收藏变得虚假,但是如果我已经收藏了收藏,那么我必须有一个很好的信息...

Anyone have an idea how to do it ? 任何人都有一个想法怎么做?

Thanks, 谢谢,

The query should look like (there should not be any table keyword): 查询应该看起来像(不应该有任何表关键字):

( (Integer) session.createQuery("select count(*) from ....").iterate().next() ).intValue(); ((Integer)session.createQuery(“从....中选择count(*)”。iterate()。next()).intValue();

Its from hibernate documentation 它来自休眠文档

无论如何,您的查询看起来有点奇怪:我认为ID是您的主键,因此请计算id = XYZ的值仅返回0或1。-也许问题出在您的查询上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM