0

我有两个表,我正在尝试使用 Hibernate 在它们之间建立一对一的映射。

ExamDetail.java

package com.hibernate.mapping;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="exam_detail")
public class ExamDetail {

    private int id;
    private String fullName;
    private int numberOfQuestions;
    private int passingPercentage;

    @Id
    @GeneratedValue
    @Column(name="id")
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getFullName() {
        return fullName;
    }
    public void setFullName(String fullName) {
        this.fullName = fullName;
    }
    public int getNumberOfQuestions() {
        return numberOfQuestions;
    }
    public void setNumberOfQuestions(int numberOfQuestions) {
        this.numberOfQuestions = numberOfQuestions;
    }
    public int getPassingPercentage() {
        return passingPercentage;
    }
    public void setPassingPercentage(int passingPercentage) {
        this.passingPercentage = passingPercentage;
    }
}

考试.java

package com.hibernate.mapping;

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

@Entity
@Table(name="exam")
public class Exam {

    private int id;
    private String shortName;
    private ExamDetail detail;

    @Id
    @GeneratedValue
    @Column(name="id")
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getShortName() {
        return shortName;
    }
    public void setShortName(String shortName) {
        this.shortName = shortName;
    }

    @OneToOne(cascade=CascadeType.PERSIST)
    @JoinColumn(name="detail_id")
    public ExamDetail getDetail() {
        return detail;
    }
    public void setDetail(ExamDetail detail) {
        this.detail = detail;
    }
}

测试映射.java

package com.hibernate.mapping;

import org.hibernate.Session;

import com.hibernate.util.HibernateUtil;

public class TestMapping {

    public static void main(String[] args) {
        Session session = HibernateUtil.beginTransaction();

        Exam exam = new Exam();
        exam.setShortName("SCJA");

        ExamDetail detail = new ExamDetail();
        detail.setFullName("Sun Certified Associate");
        detail.setNumberOfQuestions(50);
        detail.setPassingPercentage(60);

        exam.setDetail(detail);

        session.save(exam);
        HibernateUtil.commitTransaction();
    }
}

当我运行这段代码时,我遇到了以下异常:-

线程“主”org.hibernate.exception.ConstraintViolationException 中的异常:无法插入:[com.hibernate.mapping.Exam] 在 org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94) 在 org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) 在 org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java :64) 在 org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2666) 在 org.hibernate.action.EntityIdentityInsertAction 的 org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2186)。在 org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321) 在 org.hibernate.event 的 org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279) 执行(EntityIdentityInsertAction.java:71) .def.AbstractSaveEventListener。performSave(AbstractSaveEventListener.java:204) 在 org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130) 在 org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210) 在 org.hibernate .event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195) at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java :50) 在 org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:562) 在 org.hibernate.impl.SessionImpl.save(SessionImpl.java:550) 在 org.hibernate.impl.SessionImpl.save(SessionImpl.java:546) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun .reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext .java:342) at $Proxy14.save(Unknown Source) at com.hibernate.mapping.TestMapping.main(TestMapping.java:22) 原因:reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext. java:342) at $Proxy14.save(Unknown Source) at com.hibernate.mapping.TestMapping.main(TestMapping.java:22) 原因:reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext. java:342) at $Proxy14.save(Unknown Source) at com.hibernate.mapping.TestMapping.main(TestMapping.java:22) 原因: com.sybase.jdbc2.jdbc.SybSQLException:表考试中的列 detail_id 不允许空值。

在 com.sybase.jdbc2.tds.Tds.processEed(Tds.java:2535) 在 com.sybase.jdbc2.tds.Tds.nextResult(Tds.java:1916) 在 com.sybase.jdbc2.jdbc.ResultGetter.nextResult (ResultGetter.java:69) 在 com.sybase.jdbc2.jdbc.SybStatement.nextResult(SybStatement.java:201) 在 com.sybase.jdbc2.jdbc.SybStatement.nextResult(SybStatement.java:182) 在 com.sybase。 jdbc2.jdbc.SybStatement.executeLoop(SybStatement.java:1596) 在 com.sybase.jdbc2.jdbc.SybStatement.execute(SybStatement.java:1588) 在 com.sybase.jdbc2.jdbc.SybPreparedStatement.execute(SybPreparedStatement.java: 580)在 org.hibernate.id.IdentityGenerator$InsertSelectDelegate.executeAndExtract(IdentityGenerator.java:138) 在 org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57) ... 还有 22 个

我究竟做错了什么 ?

有什么遗漏的陈述吗?

4

1 回答 1

0

您需要参考 JPA 中的 ID 生成。

这里:参考

JPA 示例

身份生成

于 2012-09-24T15:43:21.820 回答