Java Hibernate.OneToMany。嵌套DTO
如果您能在嵌套子映射集合上使用投影,我将非常高兴。Java Hibernate.OneToMany。嵌套DTO,java,hibernate,projection,hibernate-criteria,Java,Hibernate,Projection,Hibernate Criteria,如果您能在嵌套子映射集合上使用投影,我将非常高兴。 代码显示了我想要实现的一切,特别是TestWrapper包含了一些实体的列表。让我们假设TestWrapper是我的DTO对象。 例如,我想从test3获取名称,并将其存储为仅包含该字段集的test3对象列表。 首先,让我们看看实体类、包装器和hibernate投影代码: @Entity public class Test { @Id @GeneratedValue(strategy = GenerationType.SEQUENCE) pri
代码显示了我想要实现的一切,特别是TestWrapper包含了一些实体的列表。让我们假设TestWrapper是我的DTO对象。
例如,我想从test3获取名称,并将其存储为仅包含该字段集的test3对象列表。
首先,让我们看看实体类、包装器和hibernate投影代码:
@Entity
public class Test {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
private String name;
@ManyToOne
Test2 test2;
@OneToMany(mappedBy = "test")
List<Test3> tests3;
public Test() {
}
public List<Test3> getTests3() {
return tests3;
}
public void setTests3(List<Test3> tests3) {
this.tests3 = tests3;
}
public Test(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Test2 getTest2() {
return test2;
}
public void setTest2(Test2 test2) {
this.test2 = test2;
}
}
@Entity
public class Test2 {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
private String name;
public Test2() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Entity
public class Test3 {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
private String name;
@ManyToOne
@JoinColumn(name="test_fk")
private Test test;
public Test3() {
}
public Test getTest() {
return test;
}
public void setTest(Test test) {
this.test = test;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@实体
公开课考试{
@身份证
@GeneratedValue(策略=GenerationType.SEQUENCE)
私人长id;
私有字符串名称;
@许多酮
Test2 Test2;
@OneToMany(mappedBy=“测试”)
列出测试3;
公开考试(){
}
公共列表getTests3(){
返回测试3;
}
公共无效设置测试3(列表测试3){
this.tests3=tests3;
}
公共测试(字符串名称){
this.name=名称;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共长getId(){
返回id;
}
公共无效集合id(长id){
this.id=id;
}
公共测试2 getTest2(){
返回test2;
}
公共void setTest2(Test2 Test2){
this.test2=test2;
}
}
@实体
公共类Test2{
@身份证
@GeneratedValue(策略=GenerationType.SEQUENCE)
私人长id;
私有字符串名称;
公共测试2(){
}
公共长getId(){
返回id;
}
公共无效集合id(长id){
this.id=id;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
}
@实体
公共类Test3{
@身份证
@GeneratedValue(策略=GenerationType.SEQUENCE)
私人长id;
私有字符串名称;
@许多酮
@JoinColumn(name=“test_fk”)
私人测试;
公共测试3(){
}
公共测试getTest(){
回归试验;
}
公共无效设置测试(测试){
这个。测试=测试;
}
公共长getId(){
返回id;
}
公共无效集合id(长id){
this.id=id;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
}
包装器:
public class TestWrapper {
private Test test;
private String test2_name;
private List<Test3> tests3;
public TestWrapper() {
}
public TestWrapper(Test test, String test2_name) {
this.test = test;
this.test2_name = test2_name;
}
public List<Test3> getTests3() {
return tests3;
}
public void setTests3(List<Test3> tests3) {
this.tests3 = tests3;
}
public Test getTest() {
return test;
}
public void setTest(Test test) {
this.test = test;
}
public String getTest2_name() {
return test2_name;
}
public void setTest2_name(String test2_name) {
this.test2_name = test2_name;
}
}
公共类TestWrapper{
私人测试;
私有字符串test2_名称;
私有列表测试3;
公共TestWrapper(){
}
公共TestWrapper(测试测试,字符串test2\u名称){
这个。测试=测试;
this.test2_name=test2_name;
}
公共列表getTests3(){
返回测试3;
}
公共无效设置测试3(列表测试3){
this.tests3=tests3;
}
公共测试getTest(){
回归试验;
}
公共无效设置测试(测试){
这个。测试=测试;
}
公共字符串getTest2_name(){
返回test2_名称;
}
公共void setTest2\u名称(字符串test2\u名称){
this.test2_name=test2_name;
}
}
和我的hibernate标准投影:
public List<TestWrapper> getTestProjection() {
Session session = entityManager.unwrap(Session.class);
Criteria test = session.createCriteria(Test.class);
test.createAlias("test2", "test2", JoinType.LEFT_OUTER_JOIN);
test.createAlias("tests3","tests3",JoinType.LEFT_OUTER_JOIN);
test.setFetchMode("tests3", FetchMode.JOIN);
// Criteria test2 = test.createCriteria("test2");
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.property("id"));
projectionList.add(Projections.property("test2.name"));
projectionList.add(Projections.property("test3.name"));
test.setProjection(projectionList);
test.setResultTransformer(new
AliasToBeanResultTransformer(TestWrapper.class));
return test.list();
}
公共列表getTestProjection(){
Session Session=entityManager.unwrap(Session.class);
条件测试=session.createCriteria(test.class);
createAlias(“test2”,“test2”,JoinType.LEFT\u OUTER\u JOIN);
createAlias(“tests3”、“tests3”、JoinType.LEFT\u OUTER\u JOIN);
setFetchMode(“tests3”,FetchMode.JOIN);
//标准test2=test.createCriteria(“test2”);
ProjectionList ProjectionList=Projections.ProjectionList();
projectionList.add(Projections.property(“id”));
projectionList.add(Projections.property(“test2.name”);
projectionList.add(Projections.property(“test3.name”);
测试集投影(投影列表);
测试设置结果变压器(新
别名BeanResultTransformer(TestWrapper.class));
返回test.list();
}
您不能这样做,您的查询不会返回实体,而只是像常规sql查询一样返回列
AliasToBeanResultTransformer只是基于列的别名将每行映射到DTO的一个实例
因此,您必须修改您的DTO,并为您的投影提供和别名或编写您自己的Transformer来重新分组行您不能这样做,您的查询不会返回实体,而是像常规sql查询一样返回列 AliasToBeanResultTransformer只是基于列的别名将每行映射到DTO的一个实例 所以您必须修改您的DTO,并为您的投影提供和别名或编写您自己的Transformer以重新分组行