外键允许为空啊。。我用的是persist()来直接保存。
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "account_id")我要插入的表是这么设的。我怎么才能插入一个指定的值给account_id他啊。

解决方案 »

  1.   

    这是一个BEAN
    @Entity
    @Name("account")
    @Scope(SESSION)
    @Table(name = "account")
    public class Account implements java.io.Serializable { static final long serialVersionUID = 2964249612552073886L; private Integer accountId;
    private String login;
    private String password;
    private Date registrationDate;
    private String acctType;
    private Set<Organization> organizations = new HashSet<Organization>(0);
    private Set<Person> persons = new HashSet<Person>(0);
    private Set<Project> projects = new HashSet<Project>(0);
    private Set<AccountContactInfo> accountContactInfos = new HashSet<AccountContactInfo>(
    0);
    private Set<ProviderReferral> providerReferrals = new HashSet<ProviderReferral>(
    0);
    private Set<PanelistProvider> panelistProviders = new HashSet<PanelistProvider>(
    0); public Account() {
    } public Account(String login, String password, Date registrationDate,
    String acctType, Set<Organization> organizations,
    Set<Person> persons, Set<Project> projects,
    Set<AccountContactInfo> accountContactInfos,
    Set<ProviderReferral> providerReferrals,
    Set<PanelistProvider> panelistProviders) {
    this.login = login;
    this.password = password;
    this.registrationDate = registrationDate;
    this.acctType = acctType;
    this.organizations = organizations;
    this.persons = persons;
    this.projects = projects;
    this.accountContactInfos = accountContactInfos;
    this.providerReferrals = providerReferrals;
    this.panelistProviders = panelistProviders;
    } @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "account_id", unique = true, nullable = false)
    public Integer getAccountId() {
    return this.accountId;
    } public void setAccountId(Integer accountId) {
    this.accountId = accountId;
    } @Column(name = "login", length = 50)
    @Length(max = 50)
    public String getLogin() {
    return this.login;
    } public void setLogin(String login) {
    this.login = login;
    } @Column(name = "password", length = 50)
    @Length(min=6 ,max = 50,message="为了您密码的安全请输入至少6位密码!")
    public String getPassword() {
    return this.password;
    } public void setPassword(String password) {
    this.password = password;
    }
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "registration_date", length = 0)
    public Date getRegistrationDate() {
    return this.registrationDate;
    } public void setRegistrationDate(Date registrationDate) {
    this.registrationDate = registrationDate;
    } @Column(name = "acct_type", length = 50)
    @Length(max = 50)
    public String getAcctType() {
    return this.acctType;
    } public void setAcctType(String acctType) {
    this.acctType = acctType;
    }
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "account")
    public Set<Organization> getOrganizations() {
    return this.organizations;
    } public void setOrganizations(Set<Organization> organizations) {
    this.organizations = organizations;
    }
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "account")
    public Set<Person> getPersons() {
    return this.persons;
    } public void setPersons(Set<Person> persons) {
    this.persons = persons;
    }
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "account")
    public Set<Project> getProjects() {
    return this.projects;
    } public void setProjects(Set<Project> projects) {
    this.projects = projects;
    }
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "account")
    public Set<AccountContactInfo> getAccountContactInfos() {
    return this.accountContactInfos;
    } public void setAccountContactInfos(
    Set<AccountContactInfo> accountContactInfos) {
    this.accountContactInfos = accountContactInfos;
    }
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "account")
    public Set<ProviderReferral> getProviderReferrals() {
    return this.providerReferrals;
    } public void setProviderReferrals(Set<ProviderReferral> providerReferrals) {
    this.providerReferrals = providerReferrals;
    }
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "account")
    public Set<PanelistProvider> getPanelistProviders() {
    return this.panelistProviders;
    } public void setPanelistProviders(Set<PanelistProvider> panelistProviders) {
    this.panelistProviders = panelistProviders;
    }}
      

  2.   

    这是另一个
    @Entity
    @Name("project")
    @Scope(SESSION)
    @Table(name = "project")
    public class Project implements java.io.Serializable { private Integer projectId;
    private Account account;
    private String projectName;
    private Byte numberOfQuestions;
    private Integer expectedNumberOfResponses;
    private Integer actualNumberOfResponses;
    private Date startDate;
    private Byte expectedDuration;
    private BigDecimal completionPercentage;
    private Date endDate;
    private BigDecimal rewardAmount;
    private String projectDesc;
    private String projectType;
    private Set<ProjectMultivalueAttribute> projectMultivalueAttributes = new HashSet<ProjectMultivalueAttribute>(
    0);
    private Set<ProjectAttributes> projectAttributeses = new HashSet<ProjectAttributes>(
    0);
    private Set<ProviderAccountDetail> providerAccountDetails = new HashSet<ProviderAccountDetail>(
    0); public Project() {
    } public Project(Account account, String projectName, Byte numberOfQuestions,
    Integer expectedNumberOfResponses, Integer actualNumberOfResponses,
    Date startDate, Byte expectedDuration,
    BigDecimal completionPercentage, Date endDate,
    BigDecimal rewardAmount, String projectDesc, String projectType,
    Set<ProjectMultivalueAttribute> projectMultivalueAttributes,
    Set<ProjectAttributes> projectAttributeses,
    Set<ProviderAccountDetail> providerAccountDetails) {
    this.account = account;
    this.projectName = projectName;
    this.numberOfQuestions = numberOfQuestions;
    this.expectedNumberOfResponses = expectedNumberOfResponses;
    this.actualNumberOfResponses = actualNumberOfResponses;
    this.startDate = startDate;
    this.expectedDuration = expectedDuration;
    this.completionPercentage = completionPercentage;
    this.endDate = endDate;
    this.rewardAmount = rewardAmount;
    this.projectDesc = projectDesc;
    this.projectType = projectType;
    this.projectMultivalueAttributes = projectMultivalueAttributes;
    this.projectAttributeses = projectAttributeses;
    this.providerAccountDetails = providerAccountDetails;
    } @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "project_id", unique = true, nullable = false)
    public Integer getProjectId() {
    return this.projectId;
    } public void setProjectId(Integer projectId) {
    this.projectId = projectId;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "account_id")
    public Account getAccount() {
    return this.account;
    } public void setAccount(Account account) {
    this.account = account;
    }
    @NotNull(message="不能为空,请正确填写!!")
    @Column(name = "project_name", length = 50)
    @Length(max = 50, message="输入的格式不对,请正确填写!!")
    public String getProjectName() {
    return this.projectName;
    } public void setProjectName(String projectName) {
    this.projectName = projectName;
    } @NotNull(message="不能为空,请正确填写!!")
    @Column(name = "number_of_questions")
    @Range(min = 0, max = 255, message="不能为空,且数值介于0到255之间,请正确填写!!" )
    public Byte getNumberOfQuestions() {
    return this.numberOfQuestions;
    } public void setNumberOfQuestions(Byte numberOfQuestions) {
    this.numberOfQuestions = numberOfQuestions;
    } @NotNull(message="不能为空,请正确填写!!")
    @Column(name = "expected_number_of_responses")
    @Range(min=0, max=255, message="不能为空,且数值介于0到255之间,请正确填写!!" )
    public Integer getExpectedNumberOfResponses() {
    return this.expectedNumberOfResponses;
    } public void setExpectedNumberOfResponses(Integer expectedNumberOfResponses) {
    this.expectedNumberOfResponses = expectedNumberOfResponses;
    } @Column(name = "actual_number_of_responses")
    public Integer getActualNumberOfResponses() {
    return this.actualNumberOfResponses;
    } public void setActualNumberOfResponses(Integer actualNumberOfResponses) {
    this.actualNumberOfResponses = actualNumberOfResponses;
    } @NotNull(message="不能为空,请正确填写!!")
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "start_date", length = 0)
    public Date getStartDate() {
    return this.startDate;
    } public void setStartDate(Date startDate) {
    this.startDate = startDate;
    } @NotNull(message="不能为空,请正确填写!!")
    @Column(name = "expected_duration")
    @Range(min=0, max=255, message="不能为空,且数值介于0到255之间,请正确填写!!" )
    public Byte getExpectedDuration() {
    return this.expectedDuration;
    } public void setExpectedDuration(Byte expectedDuration) {
    this.expectedDuration = expectedDuration;
    } @Column(name = "completion_percentage", precision = 9, scale = 4)
    public BigDecimal getCompletionPercentage() {
    return this.completionPercentage;
    } public void setCompletionPercentage(BigDecimal completionPercentage) {
    this.completionPercentage = completionPercentage;
    }
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "end_date", length = 0)
    public Date getEndDate() {
    return this.endDate;
    } public void setEndDate(Date endDate) {
    this.endDate = endDate;
    } @Column(name = "reward_amount", precision = 18, scale = 3)
    public BigDecimal getRewardAmount() {
    return this.rewardAmount;
    } public void setRewardAmount(BigDecimal rewardAmount) {
    this.rewardAmount = rewardAmount;
    } @Column(name = "project_desc")
    public String getProjectDesc() {
    return this.projectDesc;
    } public void setProjectDesc(String projectDesc) {
    this.projectDesc = projectDesc;
    } @Column(name = "project_type", length = 50)
    @Length(max = 50)
    public String getProjectType() {
    return this.projectType;
    } public void setProjectType(String projectType) {
    this.projectType = projectType;
    }
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "project")
    public Set<ProjectMultivalueAttribute> getProjectMultivalueAttributes() {
    return this.projectMultivalueAttributes;
    } public void setProjectMultivalueAttributes(
    Set<ProjectMultivalueAttribute> projectMultivalueAttributes) {
    this.projectMultivalueAttributes = projectMultivalueAttributes;
    }
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "project")
    public Set<ProjectAttributes> getProjectAttributeses() {
    return this.projectAttributeses;
    } public void setProjectAttributeses(
    Set<ProjectAttributes> projectAttributeses) {
    this.projectAttributeses = projectAttributeses;
    }
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "project")
    public Set<ProviderAccountDetail> getProviderAccountDetails() {
    return this.providerAccountDetails;
    } public void setProviderAccountDetails(
    Set<ProviderAccountDetail> providerAccountDetails) {
    this.providerAccountDetails = providerAccountDetails;
    }
    }我这样插入
    project.setProjectType("2");
    em.persist(project);
    我要怎么把Project表里的account_id这个字段设一个我给定的值啊?
      

  3.   

    你的问题主要出在数据关联,检查你要保存的对象的各个属性是否饱满例如:保存班级(班级里有个教师id号)ClassBean classBean = new ClassBean() ;
    classBean.set.....    //普通属性赋值
    Teacher teacher = new Teacher() ;
    teacher.setTid(tid)  //设置教师idclassBean.setTeacher(teacher);    //估计你没有加上这句话!!!
    //保存classBean