空的外键,在ManyToOne关系中使用hibernate [4.1.1]注释 [英] Null foreign key, in ManyToOne relation using hibernate [4.1.1] annotations

查看:132
本文介绍了空的外键,在ManyToOne关系中使用hibernate [4.1.1]注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用 Hibernate 4.1.1 持久化一对多和多对一的关系,但是外键始终是 NULL / p>

有两个实体: Account Client 。一个客户可以有多个帐户

下面是这些类(只有重要的):
$ b

Account.java

  @Entity 
@Table(name =account)
public class Account实现Serializable {
private Client client;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name =id)
public long getId(){
返回ID;

$ b @ManyToOne
@JoinColumn(name =id_client)
public Client getClient(){
return client;


code
$ b $ p $ Client.java / b>

  @Entity 
@Table(name =client)
public class Client implements Serializable {
私人列表<帐户>账户;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name =id)
public long getId(){
返回ID;


@OneToMany(mappedBy =client,cascade = CascadeType.ALL,fetch = FetchType.EAGER)
public List< Account> getAccounts(){
返回帐户;




$ p / p>

  session.beginTransaction(); 

客户端客户端=新客户端();
帐户帐户1 =新帐户();
帐户account2 =新帐户();

a.addAccount(account1);
a.addAccount(account2);

session.save(client);
session.getTransaction()。commit();

运行时,Hibernate将外键添加到表中:



Hibernate:alter table account add index FKB9D38A2D3B988D48(id_client),add constraint FKB9D38A2D3B988D48 foreign key(id_client)references client(id)



两个帐户都有id_client列 NULL



我试着把 nullable = false 在@JoinColumn关系,但只是调用一个异常。



线程main中的异常org.hibernate.exception.ConstraintViolationException: id_client'不能为空

解决方案

我忘了将客户端添加到帐户中。

  account1.setClient(client); 
account2.setClient(client);

现在可以运作了。感谢您的小费。 ;)

I am trying to persist a one-to-many and a many-to-one relation using Hibernate 4.1.1 but the foreign key is always NULL.

There are two entities: Account and Client. A Client could have multiple Accounts while an Account has exactly one Client.

Here are the classes (only what matters):

Account.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

@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

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: 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.

I tried putting nullable = false at the @JoinColumn relation but that just invoked an exception.

Exception in thread "main" org.hibernate.exception.ConstraintViolationException: Column 'id_client' cannot be null

解决方案

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. ;)

这篇关于空的外键,在ManyToOne关系中使用hibernate [4.1.1]注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆