2014-12-01 61 views
0

我正在重新編程應用程序。 當前數據庫具有像「User」這樣的表名,它是新數據庫中的保留字,所以我將表名更改爲「NewUser」。我還必須更改一些列名稱。我想代碼,以便它導入了新的名字,但在應用程序中回保留字立即改變他們,所以我沒有花很多時間重新編程的:從表中重命名JPA實體

示例代碼:

@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; 
} 

回答

0

只需將@Table(name = "NewUser")添加到您的實體。它將重新映射實體到新的表名稱,但將User保存爲實體名稱,這是查詢中使用的名稱。你只需要重寫原生查詢,因爲這是純SQL。此外,重命名列名使用@Column(name = "newuid")