我现在正在学习 Hibernate ORM(v. 3),我有一个问题。我有一个名为 USERS 的表,使用注释创建:
package com.hibernatedb.entities;
import javax.persistence.*;
@Entity
@Table(name = "USERS",uniqueConstraints = {@UniqueConstraint(columnNames={"USER_LOGIN", "USER_EMAIL"})})
public class User {
@Column(name = "USER_LOGIN", length=80, nullable=false)
private String login;
@Column(name = "USER_PASS", length=80, nullable=false)
private String password;
@Column(name = "USER_EMAIL", length=80, nullable=false)
private String email;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "USER_ID", nullable=false)
private Long id;
...
// some getters and setters, toString() and other stuff
...
}
和一个产品实体:
@Entity
@Table(name = "PRODUCTS",uniqueConstraints = {@UniqueConstraint(columnNames={"PRODUCT_ID", "PRODUCT_NAME"})})
public class Product {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="PRODUCT_ID")
private long id;
@Column(name="PRODUCT_NAME", length=85, nullable=false)
private String name;
@Column(name="PRODUCT_DESCRIPTION", columnDefinition="mediumtext", length=1000)
private String description;
@Column(name="PRODUCT_COST", nullable=false)
private double cost;
所以我的问题是:如何创建一个名为“USER +
用户.getId()
BUYS”,其中包含实体(表记录)“用户”中的用户的 2 个外键(USER_ID 和 PRODUCT_ID),无需创建原始 SQL 表,但使用 Hibernate 注释或 XML 映射。所以我想要类似的东西
public class TransactionEntityBulider() {
public TransactionEntityBulder(User user)
// something that build me "USER + User.getId() BUYS" table and
}
public TransactionEntity getEntity() {
// something that return a "USER + User.getId() BUYS" table entity
}
我也想看看其他一些方法来解决我的问题。