JPA @OrderBy与Hibernate

huangapple go评论110阅读模式
英文:

JPA @OrderBy with Hibernate

问题

I'm here to help with the translation. Here's the translated code part:

我正在使用Hibernate并想要使用`@OrderBy`来对`resultSet`进行排序

public class TopLevelEntity extends Entity {
	@OneToMany(mappedBy = "topLevelEntity", fetch = FetchType.LAZY, 
               cascade = CascadeType.ALL, orphanRemoval = true)
	private Set<TopLevelEntityTranslation> translations;
	//other fields
}

public class NextLevelEntity extends Entity {
	@OneToMany(mappedBy = "nextLevelEntity", fetch = FetchType.LAZY, 
               cascade = CascadeType.ALL, orphanRemoval = true)
	private Set<NextLevelEntityTranslation> translations;

	@Column
	private Long number;
	//other fields
}

public class TopLevelEntityTranslation extends Entity {
	@ManyToOne
	@JoinColumn(name = "TOP_LVL_ENTITY_ID")
	private TopLevelEntity topLevelEntity;
	
	@OrderBy("nextLevelEntity.number")
	@OneToMany(mappedBy = "topLevelEntityTranslation", fetch = FetchType.LAZY, 
               cascade = CascadeType.ALL, orphanRemoval = true)
	private Set<NextLevelEntityTranslation> nextLevelEntityTranslations;
	//other fields
}

public class NextLevelEntityTranslation extends Entity {
	@ManyToOne
	@JoinColumn(name = "TOP_LVL_ENTITY_TR_ID")
	private TopLevelEntityTranslation topLevelEntityTranslation;

	@ManyToOne
	@JoinColumn(name = "NEXT_LVL_ENTITY_ID")
	private NextLevelEntity nextLevelEntity;
	//other fields
}

If you have any more questions or need further assistance, please let me know.

英文:

Im using hibernate and want to use @OrderBy to order a resultSet:

public class TopLevelEntity extends Entity {
	@OneToMany(mappedBy = &quot;topLevelEntity&quot;, fetch = FetchType.LAZY, 
               cascade = CascadeType.ALL, orphanRemoval = true)
	private Set&lt;TopLevelEntityTranslation&gt; translations;
	//other fields
}

public class NextLevelEntity extends Entity {
	@OneToMany(mappedBy = &quot;nextLevelEntity&quot;, fetch = FetchType.LAZY, 
               cascade = CascadeType.ALL, orphanRemoval = true)
	private Set&lt;NextLevelEntityTranslation&gt; translations;

	@Column
	private Long number;
	//other fields
}

public class TopLevelEntityTranslation extends Entity {
	@ManyToOne
	@JoinColumn(name = &quot;TOP_LVL_ENTITY_ID&quot;)
	private TopLevelEntity topLevelEntity;
	
	@OrderBy(&quot;nextLevelEntity.number&quot;)
	@OneToMany(mappedBy = &quot;topLevelEntityTranslation&quot;, fetch = FetchType.LAZY, 
               cascade = CascadeType.ALL, orphanRemoval = true)
	private Set&lt;NextLevelEntityTranslation&gt; nextLevelEntityTranslations;
	//other fields
}

public class NextLevelEntityTranslation extends Entity {
	@ManyToOne
	@JoinColumn(name = &quot;TOP_LVL_ENTITY_TR_ID&quot;)
	private TopLevelEntityTranslation topLevelEntityTranslation;

	@ManyToOne
	@JoinColumn(name = &quot;NEXT_LVL_ENTITY_ID&quot;)
	private NextLevelEntity nextLevelEntity;
	//other fields
}

I have an entity hierarchy like shown above. But @OrderBy annotation(no the annotation ofc, but the statement generated by hibernate) throws an exception. Exception main part:

> missing FROM-clause entry for table "nextLevelEntity"

There are few examples of using jpa @OrderBy but pattern of those that I found is the same with mine.
Read that @OrderBy doesn't work well with Hibernate's JPA implementation. But the question was asked 7 years ago. Is it true or i have a mistake in my code?

答案1

得分: 3

根据@OrderBy文档

点号(".")符号用于引用嵌入属性中的属性。与点号符号一起使用的每个标识符的值都是相应嵌入字段或属性的名称。

示例:

@Entity
public class Person {
   ...
   @ElementCollection
   @OrderBy("zipcode.zip, zipcode.plusFour")
   public Set<Address> getResidences() {...};
   ...
}

@Embeddable
public class Address {
  protected String street;
  protected String city;
  protected String state;

  @Embedded
  protected Zipcode zipcode;
}

@Embeddable
public class Zipcode {
   protected String zip;
   protected String plusFour;
}

所以,对于您的情况,您不能使用它。

英文:

Actually, according to the documentation for the @OrderBy:

> The dot (".") notation is used to refer to an attribute within an embedded attribute. The value of each identifier used with the dot notation is the name of the respective embedded field or property.

Example:

@Entity 
public class Person {
   ...
   @ElementCollection
   @OrderBy(&quot;zipcode.zip, zipcode.plusFour&quot;)
   public Set&lt;Address&gt; getResidences() {...};
   ...
}
  
@Embeddable 
public class Address {
  protected String street;
  protected String city;
  protected String state;

  @Embedded
  protected Zipcode zipcode;
}

@Embeddable 
public class Zipcode {
   protected String zip;
   protected String plusFour;
}

So, you can not use it for your case.

huangapple
  • 本文由 发表于 2020年8月3日 19:45:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63228831.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定