Java Null 外键,在 ManyToOne 关系中使用 hibernate [4.1.1] 注释
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9850566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Null foreign key, in ManyToOne relation using hibernate [4.1.1] annotations
提问by Lauren?iu Onac
I am trying to persist a one-to-many and a many-to-one relation using Hibernate 4.1.1but the foreign key is always NULL.
我正在尝试使用Hibernate 4.1.1保持一对多和多对一的关系,但外键始终为NULL。
There are two entities: Accountand Client. A Clientcould have multiple Accountswhile an Accounthas exactly one Client.
有两个实体:Account和Client。一个客户可以有多个账户,而一个账户只有一个客户。
Here are the classes (only what matters):
以下是课程(仅重要的内容):
Account.java
账号.java
@Entity
@Table(name = "account")
public class Account implements Serializable {
private Client client;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
public long getId() {
return id;
}
@ManyToOne
@JoinColumn(name = "id_client")
public Client getClient() {
return client;
}
}
Client.java
客户端.java
@Entity
@Table(name = "client")
public class Client implements Serializable {
private List<Account> accounts;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
public long getId() {
return id;
}
@OneToMany(mappedBy = "client", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
public List<Account> getAccounts() {
return accounts;
}
}
Test.java
测试.java
session.beginTransaction();
Client client = new Client();
Account account1 = new Account();
Account account2 = new Account();
a.addAccount(account1);
a.addAccount(account2);
session.save(client);
session.getTransaction().commit();
While running, Hibernate adds the foreign key to the table:
在运行时,Hibernate 将外键添加到表中:
Hibernate: alter table account add index FKB9D38A2D3B988D48 (id_client), add constraint FKB9D38A2D3B988D48 foreign key (id_client) references client (id)
Hibernate: alter table account add index FKB9D38A2D3B988D48 (id_client), add constraint FKB9D38A2D3B988D48 foreign key (id_client) references client (id)
Both accounts have id_client column NULL.
两个帐户都有 id_client 列NULL。
I tried putting nullable = falseat the @JoinColumn relation but that just invoked an exception.
我尝试将nullable = false放在 @JoinColumn 关系中,但这只是调用了一个异常。
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: Column 'id_client' cannot be null
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: Column 'id_client' cannot be null
采纳答案by Lauren?iu Onac
Figured it out. I forgot to add the client to the accounts.
弄清楚了。我忘了将客户添加到帐户中。
account1.setClient(client);
account2.setClient(client);
Now it works. Thank you for the tips. ;)
现在它起作用了。感谢您的小费。;)
回答by kandarp
I think issue is that you need to save account first and in the last client will be saved.
我认为问题是您需要先保存帐户,然后才能保存最后一个客户。