我正在邁出進入 jpa 的第一步(將整個資料庫從 jdbc 移植到 jpa),我想知道如何實作以下目標:我有兩個表,一個用戶表和一個 ProfileImages 表,ProfileImages 表包含在一個 FK 中user_id 然后是另一個欄位,它是一個位元組陣列(保存影像的位元組)。
我想要實作的是能夠直接在我的用戶模型中恢復位元組陣列,內容如下:
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "users_userid_seq")
@SequenceGenerator(name = "users_userid_seq", sequenceName = "users_userid_seq", allocationSize = 1)
private Long userId;
@Column
private String name;
@Column
private String surname;
@Column(nullable = false, unique = true)
private String username;
@Column(nullable = false, unique = true)
private String email;
@Column
private String password;
@Column(nullable = false, unique = true)
private Integer fileNumber;
@Column
private boolean isAdmin;
// Map the byte array from the profile_image relation
private byte[] image;
.....
.....
}
注意:最好不要更改架構以使用戶持有位元組陣列。
uj5u.com熱心網友回復:
您可以使用SecondaryTable注釋將兩個表映射到一個Entity:
@Entity
@Table(name = "users")
@SecondaryTable(name = "profileimages",
pkJoinColumns = @PrimaryKeyJoinColumn(name = "user_id"))
public class User {
@Column(name = "image", table = "profileimages")
private byte[] image;
另請查看檔案:https : //docs.jboss.org/hibernate/orm/5.5/userguide/html_single/Hibernate_User_Guide.html#sql-custom-crud-secondary-table-example
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/321959.html
