仅在JPA,Hibernate中连接具有表名和列名的表

时间:2015-05-17 20:52:55

标签: hibernate jpa

仅在JPA,Hibernate中连接具有表名和列名的表。

我有两个实体用户(在UserService中)和地址(在AddressService中),现在我想要连接地址表到users表而不使用用户实体,只是使用users表和user_id作为外键。

@Entity
@DynamicUpdate
@Table(name = "users", schema = "security", uniqueConstraints = { @UniqueConstraint(columnNames = { "email" }) })
@JsonPropertyOrder({ "user_id" })
public class User implements Serializable {


@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id")
private Long userId;
private String email;
private String password;

//getters and setters
}

@Entity
@Table(name = "address", schema = "account")
@SecondaryTable(name = "users", schema = "account", pkJoinColumns = { @PrimaryKeyJoinColumn(name = "id") })
@JsonPropertyOrder({ "id" })
@DynamicUpdate
public class AddressModel implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @NotNull
    @Valid
    @Embedded
    private Address address;

@ManyToOne(cascade = CascadeType.ALL)
@JoinTable(name = "users", schema = "account", joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "user_id"))
public Long user_id;  // this is the primary key in users table 
                      // and want to make foreign key in addresses table

这两个权利属于两种不同的RESTful Web服务。如何在不创建实体或具有依赖性的情况下加入这两列?

0 个答案:

没有答案