我有一个学生班,还有一个具有@OneToMany关系的帐户班。一个学生可以使用多个帐户。我的两个表格均已创建。我的帐户表中有一个字段Student_id,但是值即将为空。您能帮我做错什么吗?

    import java.util.Collection;
    import java.util.Set;

    import javax.persistence.CascadeType;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.JoinTable;
    import javax.persistence.ManyToMany;
    import javax.persistence.OneToMany;
    import javax.persistence.OneToOne;
    import javax.persistence.Table;

    @Entity
    @Table(name = "Student_Information")
    public class Student {

        @Id
        private int id;

        @Column
        private String name;


        @OneToOne(targetEntity=Address.class , cascade = CascadeType.ALL)
        @JoinColumn(name="addressId" , referencedColumnName = "addressId")
        private Address adId;

        @OneToMany(mappedBy="student",cascade= CascadeType.ALL)
        private Collection<Account> account;


        @ManyToMany(cascade = CascadeType.ALL)
        @JoinTable(name = "STUDENT_CERTIFICATION", joinColumns = { @JoinColumn(name = "id") }, inverseJoinColumns = { @JoinColumn(name = "certId") })
        private Collection<Certification> certId;


        public Collection<Account> getAccount() {
            return account;
        }

        public void setAccount(Collection<Account> account) {
            this.account = account;
        }

        public Collection<Certification> getCertId() {
            return certId;
        }

        public void setCertId(Collection<Certification> certId) {
            this.certId = certId;
        }


        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Address getAdId() {
            return adId;
        }

        public void setAdId(Address adId) {
            this.adId = adId;
        }

    }

Account Class




import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "Account")
public class Account {

    @Id
    private int accountId;


    @Column
    private String accountNumber;

    @ManyToOne
    @JoinColumn(name="id")
    private Student id;

    public Student getId() {
        return id;
    }

    public void setId(Student id) {
        this.id = id;
    }

    public int getAccountId() {
        return accountId;
    }

    public void setAccountId(int accountId) {
        this.accountId = accountId;
    }

    public String getAccountNumber() {
        return accountNumber;
    }

    public void setAccountNumber(String accountNumber) {
        this.accountNumber = accountNumber;
    }


}


我有一个学生班,还有一个具有@OneToMany关系的帐户班。一个学生可以使用多个帐户。我的两个表格均已创建。我的帐户表中有一个字段Student_id,但是值即将为空。您能帮我做错什么吗?

最佳答案

我认为问题是因为联接列注释中的名称,此名称应与表中列的实际名称匹配,在这种情况下,如您所说的student_id

@ManyToOne
@JoinColumn(name="student_id")
private Student id;

关于java - hibernate 中的OneToMany映射。值将为空,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61185365/

10-10 15:05