从表重命名JPA实体

时间:2014-12-01 20:51:55

标签: jpa ejb

我正在重新编写应用程序。  当前数据库的表名称为“User”,这是新数据库中的保留字,因此我将表名更改为“NewUser”。我还必须更改几个列名。我想对它进行编码,以便导入新名称,但在应用程序中立即将它们更改为保留字,这样我就不必花费大量时间重新编程:

示例代码:

@Entity 
 // NewUser is the new table name but still User below. I would like to keep the user 
 //as the class name but go after NewUser in the db 
 public class User implements java.io.Serializable { 
 private static final long serialVersionUID = -6091824661950209090L; 
 /** Primary key */ 
 @Id 
 @GeneratedValue(strategy = GenerationType.AUTO) 

 // uid is now newuid in the table but again I want to keep uid in the app 
 //but reference newuid from the db 
 protected int uid; 

 public int getUid() { 
 return this.uid; 
 } 

 public void setUid(int uid) { 
 this.uid = uid; 
 } 

1 个答案:

答案 0 :(得分:0)

只需将@Table(name = "NewUser")添加到您的实体即可。它会将实体重新映射到新表名,但保留User作为查询中使用的实体名称。如果你拥有它们,你只需要重写本机查询,因为那是纯SQL。另外,要重命名列名,请使用@Column(name = "newuid")