数据在Spring Boot中的联接查询中有值,但仍为空。

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

Data coming Null even when join query is having values in Spring Boot

问题

我有一个名为Table Sellars的依赖项,其中仅包含City ID。但在显示销售商时,我需要显示销售商的国家和州。我已经编写了连接查询,它返回销售商的其余详细信息,除了国家、城市和州都为空。我没有保留任何类似@manytoOne的关系,因为它还会在城市下显示邮政编码列表,因为存在关系。

@Entity(name = "tbl_sellars")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Sellars extends CommonEntity {

    @Column(name = "sellarName")
    String sellarName;
    @JsonIgnore
    @Column(name = "fk_city_id") // this contains the city Id
    String fk_city_id;

    @Transient // 将这些变量设置为Transient,因为在实体中不需要它们
    String country;
    @Transient
    @JsonProperty
    String state;
    @Transient
    @JsonProperty
    String cityName;
}

@Entity(name = "tbl_city")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class City extends CommonEntity {

    String cityName;

    @ManyToOne
    @JoinColumn(name = "fk_state_id")
    private State state;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "fk_city_id", referencedColumnName = "id")
    private List<ZipCode> zipcodes;
}

@Repository
public interface SellarsRepository extends JpaRepository<Sellars, Long> {
    @Query(value = "SELECT u.*, c.country, s.state, ci.cityName" +
            " FROM tbl_sellars u" + " JOIN tbl_city ci ON u.fk_city_id = ci.id" +
            " JOIN tbl_state s ON ci.fk_state_id = s.id" +
            " JOIN tbl_country c ON s.fk_country_id = c.id", nativeQuery = true)
    List<Sellars> getAllSellarsWithRegion();
}

返回的JSON如下:

[
  {
    "id": 2,
    "sellarName": "Avik ray",
    "country": null,
    "state": null,
    "cityName": null
  }
]
英文:

I have a dependency Table Sellars which is having the City ID only. But while showing the sellars I need to show the Sellar country, state. I have written the join query it is returning rest of the sellar details except country,city and state as null. I haven't kept any relation like @manytoOne as it will also show the zipcode list under the city as there is a relation.

@Entity(name = &quot;tbl_sellars&quot;)
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = &quot;id&quot;)
public class Sellars extends CommonEntity {
	
	@Column(name = &quot;sellarName&quot;)
	String sellarName;
	@JsonIgnore
	@Column(name = &quot;fk_city_id&quot;)   // this contains the city Id
	String fk_city_id;
	
	@Transient             //Kept these variable Transient as I Don&#39;t need these in Entity
	String country;
	@Transient
	@JsonProperty
	String state;
	@Transient
	@JsonProperty
	String cityName;
}


@Entity(name= &quot;tbl_city&quot;)
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = &quot;id&quot;)
public class City extends CommonEntity {
	
	String cityName;
	
	@ManyToOne
	@JoinColumn(name = &quot;fk_state_id&quot;)
	private State state;
	
	@OneToMany(cascade = CascadeType.ALL)
	@JoinColumn(name = &quot;fk_city_id&quot;, referencedColumnName = &quot;id&quot;)
	private List&lt;ZipCode&gt; zipcodes;
}

@Repository
public interface SellarsRepository extends JpaRepository&lt;Sellars, Long&gt;{
	  @Query(value = &quot;SELECT u.*, c.country, s.state,ci.cityName&quot; +
	  &quot; FROM tbl_sellars u&quot; + &quot; JOIN tbl_city ci ON u.fk_city_id = ci.id&quot; +
	  &quot; JOIN tbl_state s ON ci.fk_state_id = s.id&quot; +
	  &quot; JOIN tbl_country c ON s.fk_country_id = c.id&quot;, nativeQuery = true)
	  List&lt;Sellars&gt; getAllSellarsWithRegion();
}

Json Returned like

[
  {
    &quot;id&quot;: 2,
    &quot;sellarName&quot;: &quot;Avik ray&quot;,
    &quot;country&quot;: null,
    &quot;state&quot;: null,
    &quot;cityName&quot;: null
  }
]

答案1

得分: 1

由于您的属性已使用@Transient进行了注释,它们将不会被Hibernate处理,因此将为null。

英文:

As your attributes are annotated with @Transient they will not be handled by Hibernate and are therefore null

答案2

得分: 1

@Transient 注解用于标记字段在映射框架中为临时的,这意味着使用 @Transient 标记的字段将被映射框架忽略,并且该字段不会映射到任何数据库列(在关系数据库管理系统中)或文档属性(在NoSQL中)。因此,该属性不会被持久化到数据存储中。

来源:https://javabydeveloper.com/using-transient-in-spring-boot-examples/

英文:

@Transient annotation is used to mark a field to be transient for the mapping framework, which means the field marked with @Transient is ignored by mapping framework and the field not mapped to any database column (in RDBMS) or Document property (in NOSQL). Thus the property will not be persisted to data store.

Source: https://javabydeveloper.com/using-transient-in-spring-boot-examples/

huangapple
  • 本文由 发表于 2023年6月13日 18:51:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76464142.html
匿名

发表评论

匿名网友

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

确定