我有以下两个具有一对多关系的pojo,我的查询是,现在可以通过SqlResultSetMapping来获取Com的第一类的结果,但是在Com内也有SubCom的引用,但由于我当前获得的SqlResultSetMapping输出是完美的,但它包含SubCom的null,因此请告知我如何修改SqlResultSetMapping以获取SubCom的值,我应该为SubCom设计单独的SqlResultSetMapping。
第一个Pojo如下所示
@SqlResultSetMapping(
name = "firstQuery",
classes = @ConstructorResult(
targetClass = com.entity.Com.class,
columns = {
@ColumnResult(name = "com_Id" , type = Integer.class),
@ColumnResult(name = "Cescription" , type = String.class),
@ColumnResult(name = "name" , type = String.class)}))
@Entity
@Data
@Table(name = "COM")
public class Com implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "com_Id")
private Integer ComId;
private String Cescription;
private String name;
@OneToMany(mappedBy = "Com", cascade = CascadeType.ALL)
@ToString.Exclude
private List<SubCom> subCom = new ArrayList<>(); // *** Has a refrence
//**** Constructor
public Com( Integer ComId , String Cescription, String name) {
this.ComId=ComId;
this.Cescription = Cescription;
this.name = name;
}
}
我的第二个pojo是
@Entity
@Table(name = "sub_com")
@Data
public class SubCom implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "sub_com_id")
private Integer Id;
private String name;
private String Cescription;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
@JoinColumn(name = "com_Id")
private Com com; //****** Refernce to above table.
}
我已经执行了如下所示的本地sql查询
SELECT
c.com_Id,
c.Cescription,
sc.sub_domain_id
FROM Com AS c
INNER JOIN SubCom AS sc ON c.com_Id = sc.com_Id
答案 0 :(得分:0)
您必须选择所有必要的列以构造两个实体。
比这个例子中的映射更像:
@NamedNativeQuery(
name = "find_person_with_phones_by_name",
query =
"SELECT " +
" pr.id AS \"pr.id\", " +
" pr.name AS \"pr.name\", " +
" pr.nickName AS \"pr.nickName\", " +
" pr.address AS \"pr.address\", " +
" pr.createdOn AS \"pr.createdOn\", " +
" pr.version AS \"pr.version\", " +
" ph.id AS \"ph.id\", " +
" ph.person_id AS \"ph.person_id\", " +
" ph.phone_number AS \"ph.number\", " +
" ph.phone_type AS \"ph.type\" " +
"FROM Person pr " +
"JOIN Phone ph ON pr.id = ph.person_id " +
"WHERE pr.name LIKE :name",
resultSetMapping = "person_with_phones"
)
@SqlResultSetMapping(
name = "person_with_phones",
entities = {
@EntityResult(
entityClass = Person.class,
fields = {
@FieldResult( name = "id", column = "pr.id" ),
@FieldResult( name = "name", column = "pr.name" ),
@FieldResult( name = "nickName", column = "pr.nickName" ),
@FieldResult( name = "address", column = "pr.address" ),
@FieldResult( name = "createdOn", column = "pr.createdOn" ),
@FieldResult( name = "version", column = "pr.version" ),
}
),
@EntityResult(
entityClass = Phone.class,
fields = {
@FieldResult( name = "id", column = "ph.id" ),
@FieldResult( name = "person", column = "ph.person_id" ),
@FieldResult( name = "number", column = "ph.number" ),
@FieldResult( name = "type", column = "ph.type" ),
}
)
}
),
请在Hibernate文档中找到更多信息:https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html