0
我正在學習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()
購買」,它包含3 NS一2個的外鍵(USER_ID和PRODUCT_ID)用戶在實體(表記錄)「用戶」 沒有原始的SQL表創建,但使用Hibernate註釋或XML mapping.So我想有像
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
}
另外我想看看一些其他方法來解決我的問題。
您的意思是您要創建表USER_BUYS(其中包含USER_ID和PRODUCT_ID)? – Phan 2012-08-09 12:28:34
不,我想爲表USERS中的每個記錄創建一個名稱爲「USER」+ User.getId()+「BUYS」的表。這些表將包含USER_ID和PRODUCT_ID外鍵。 – teddypickerfromul 2012-08-09 13:05:57