英文:
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 = "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 //Kept these variable Transient as I Don't need these in Entity
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 Returned like
[
{
"id": 2,
"sellarName": "Avik ray",
"country": null,
"state": null,
"cityName": 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/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论