在模型类中,我有isActive字段,其中是boolean,表示MySql DB中的is_active字段。这是整个模型类:
package ca.gatin.model;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "Account")
public class Account {
@Id
@GeneratedValue
private Long id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(nullable = false, unique = true)
private String email;
@Column(nullable = false)
private String password;
@Column(name = "is_active", nullable = false)
private boolean isActive;
@Column(name = "date_created")
private Date dateCreated;
@Column(name = "date_last_modified")
private Date dateLastModified;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean isActive() {
return isActive;
}
public void setActive(boolean isActive) {
this.isActive = isActive;
}
public Date getDateCreated() {
return dateCreated;
}
public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}
public Date getDateLastModified() {
return dateLastModified;
}
public void setDateLastModified(Date dateLastModified) {
this.dateLastModified = dateLastModified;
}
}
但是,当我提取帐户时,请通过REST API说:
@RequestMapping(
value = "/{id}",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public ServiceResponse<Account> getAll(@PathVariable("id") Long id) {
ServiceResponse<Account> serviceResponse = accountService.getAccountById(id);
return serviceResponse;
}
在一个回复对象中,我将isActive字段由Hibernate重命名为&#34; active&#34;像这样:
{
"id": 19,
"firstName": "Julia",
"lastName": "Sarandi",
"email": "julia@gatin.ca",
"password": "111111",
"dateCreated": 1451293826000,
"dateLastModified": null,
"active": true
}
为什么呢?为什么所有其他字段的名称与Account类保持一致,但isActive是否已重命名?
这是一个问题,另一个问题是: 我是Hibernate的新手,我不明白为什么在Hibernate DB请求的日志中会显示一些奇怪的查询:
Hibernate: select account0_.id as id1_0_0_, account0_.date_created as date_cre2_0_0_, account0_.date_last_modified as date_las3_0_0_, account0_.email as email4_0_0_, account0_.first_name as first_na5_0_0_, account0_.is_active as is_activ6_0_0_, account0_.last_name as last_nam7_0_0_, account0_.password as password8_0_0_ from Account account0_ where account0_.id=?
它是什么查询语言?什么是符号:&#34; 0 _&#34;,&#34; 0_0 _&#34;。我可以切换日志以显示MySQL查询以使其更容易理解吗?
FYI 在我的application.properties文件中,我有以下配置:
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.DefaultNamingStrategy
答案 0 :(得分:1)
这与Hibernate无关,而且与你的JSON marshaller有关。 Spring使用Jackson,而Jackson使用bean属性(即getter)来访问数据并将它们转换为JSON字段。您的getter名为isActive()
,因此对应于名为active
的bean属性,因此是JSON中属性的名称。
如果您希望将JSON字段命名为isActive
,那么您的getter应为isIsActive()
。或者更好,你应该用@JsonProperty("isActive")
注释它。
要回答第二个问题,查询是一个由Hibernate生成的SQL查询。它更改表的名称并将别名分配给列主要用于消除歧义表,以及可能具有相同名称的不同表的字段AFAIK。
答案 1 :(得分:1)
将isActive字段的getter和setter方法名称更改为:
public boolean getIsActive() {
return isActive;
}
public void setIsActive(boolean isActive) {
this.isActive = isActive;
}
然后它返回isActive作为回应。