Rom*_*man 1 java spring jpa spring-data spring-data-jpa
我完全被我希望开箱即用的东西搞糊涂了.所以,要么我做了一些完全错误的事情,要么这只是一个误解.
我试图在JPA Entity类中使用getter/setter注释.我坚持在JPA维基上找到的一个例子(s.http://en.wikibooks.org/wiki/Java_Persistence/Basic_Attributes#Conversion).该示例如下所示:
@Entity
public class Employee {
...
private boolean isActive;
...
@Transient
public boolean getIsActive() {
return isActive;
}
public void setIsActive(boolean isActive) {
this.isActive = isActive;
}
@Basic
private String getIsActiveValue() {
if (isActive) {
return "T";
} else {
return "F";
}
}
private void setIsActiveValue(String isActive) {
this.isActive = "T".equals(isActive);
}
}
Run Code Online (Sandbox Code Playgroud)
我采用了最清晰,最干净的spring-data-jpa示例:http://spring.io/guides/gs/accessing-data-jpa/.
我从git中检查出来并更改了他们的示例实体类(s.https://www.chithub.com/spring-guides/gs-accessing-data-jpa/blob/master/complete/src/main/java/hello/Customer .java)看起来如下:
@Entity
public class Customer {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
private String firstName;
private String lastName;
protected Customer() {}
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
@Override
public String toString() {
return String.format(
"Customer[id=%d, firstName='%s', lastName='%s']",
id, firstName, lastName);
}
@Transient
private boolean isActive;
@Transient
public boolean getIsActive() {
return isActive;
}
public void setIsActive(boolean isActive) {
this.isActive = isActive;
}
@Column
private String getIsActiveValue() {
if (isActive) {
return "T";
} else {
return "F";
}
}
private void setIsActiveValue(String isActive) {
this.isActive = "T".equals(isActive);
}
}
Run Code Online (Sandbox Code Playgroud)
现在没什么变化.不会创建相应的字符串字段.创建数据库表的日志中的行仍然如下所示:
17:11:10.540 [main] DEBUG o.h.tool.hbm2ddl.SchemaUpdate - create table Customer (id bigint generated by default as identity, firstName varchar(255), lastName varchar(255), primary key (id))
Run Code Online (Sandbox Code Playgroud)
我完全不知道这可能是什么原因.我找不到spring-data-jpa不允许在getter上注释的文档.
任何帮助将非常非常感谢!
我认为你只是简单地混合了注释:你必须注释字段或getter,但不能同时注释两者.一旦您决定注释您的ID字段,您必须注释所有字段(而不是getter),反之亦然:如果您注释了您的getId()方法,则必须注释所有方法.
| 归档时间: |
|
| 查看次数: |
2478 次 |
| 最近记录: |