2011-07-04 52 views
0

我是Hibernate的新手,我在以下情況下有問題。更新表A後休眠更新表B

用戶類

 
@Entity 
@Table (name="user_table") 
public class User implements Serializable{ 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    +getters and setters  
    //access by field 
    //primary key 
    @Id 
    @Column (name = "username") 
    private String username; 
    @Column (name = "password") 
    private String password; 
    @Column (name = "email") 
    private String email; 
    /** 
    * Cascade all operations 
    */ 
    @OneToMany(cascade = CascadeType.ALL) 
    @JoinTable(name = "user_history_table", 
      joinColumns = { @JoinColumn(name = "username") }, 
      inverseJoinColumns = { @JoinColumn(name = "history_id") } 
    ) 
    private Set userHistory = new HashSet(0); 
} 

歷史課

 
@Entity 
@Table (name="history_table") 
public class History { 

    +getters and setters 
    //primary key 
    @Id 
    @Column (name = "history_id") 
    private int id; 
    @Column (name="url") 
    private String url; 
    @Column (name="region") 
    private String region; 
    @Column (name="source") 
    private String source; 
    @Column (name="target") 
    private String target; 
    @Column (name="cost") 
    private double cost; 
    @Column (name="balance") 
    private double balance; 
} 

的Schemate

 
create table user_table(
username varchar(50) NOT NULL PRIMARY KEY, 
password varchar(20), 
email varchar(150) 
); 

create table history_table(
history_id INTEGER AUTO_INCREMENT PRIMARY KEY, 
url varchar(1000) NOT NULL, 
cur_timestamp timestamp default now(), 
region varchar (100) NOT NULL, 
source varchar(30) NOT NULL, 
target varchar(30) NOT NULL, 
cost decimal (10,2) NOT NULL, 
balance decimal (10,2) NOT NULL 
); 

create table user_history_table(
user_history_id INTEGER AUTO_INCREMENT PRIMARY KEY, 
username varchar(50) NOT NULL, 
history_id INTEGER NOT NULL, 
FOREIGN KEY (username) REFERENCES user_table(username), 
FOREIGN KEY (history_id) REFERENCES history_table(history_id) 
); 

應用CL屁股

 
public class App { 

    public static void main(String [] args){ 
     SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); 

     Session session = sessionFactory.getCurrentSession(); 

     Transaction tx = session.beginTransaction();   
     System.out.println("Inserting Record"); 
     History history = new History();  
     history.setBalance(1000); 
     history.setCost(50.99); 
     history.setRegion("region"); 
     history.setSource("Source_test"); 
     history.setTarget("Target_test"); 
     history.setUrl("http://stackoverflow.com/"); 
     session.save(history); 
     tx.commit(); 
     System.out.println("Done"); 
    } 
} 

App.java將插入到history_table的條目,但我還需要user_history_table進行過更新。所以,我應該創建另一個對象,說user_history_obj並更新它就像history_table更新或有休眠的方式做它。 Thnaks。

回答

0

UserHistory之間的關係看起來有點像多對多的user_history作爲連接表。

你可能會更好地映射它。請參閱Hibernate Annotations Reference on mapping associations中的@ManyToMany部分。

這將改變你的user_history表的佈局,通過刪除它的自動生成的主鍵。但這可能值得一試,而且這可能會使表格以合理的方式填充這種關係。

編輯:

我不知道現在爲什麼我認爲許多到很多,但它映射一個一對多帶連接表可能是罰款。一旦創建了History對象,如果將其添加到User對象的userHistory集,則user_history_table條目將被創建並保存爲User對象的merge

+0

謝謝,@ManyToMany是有道理的,但我不認爲user_history_id主要需要被刪除。 – user200340

+0

不,它可能不必被刪除,但我認爲如果你使用休眠來生成你的DDL,它不會被生成。 –

+0

不,對不起。這並非多對多,因爲與該用戶關聯的history_table中存在餘額,時間戳和成本列。 – user200340