休眠-一对一单向注释

时间:2018-07-11 13:15:27

标签: java hibernate

以下是我创建的表

create table instructor_detail(  
  id    number(11) NOT NULL, 
  youtube_channel varchar2(128),
  hobby varchar2(45),
  CONSTRAINT instructor_detail_pk PRIMARY KEY (id)
  );

另一个表作为“讲师”表,如下所示

create table instructor(  
  id    number(11,0) NOT NULL, 
  first_name varchar2(45),
  last_name varchar2(45),
  email varchar2(45),
  instructor_detail_id number(11),
  CONSTRAINT instructor_pk PRIMARY KEY (id),
  CONSTRAINT fk_instructor FOREIGN KEY (instructor_detail_id) REFERENCES instructor_detail(id)
  );

创建了以下序列以自动生成ID和触发器 对于讲师(序列和触发器)

CREATE SEQUENCE instructor_sequence start with 1;

  CREATE OR REPLACE TRIGGER instructor_on_insert
  BEFORE INSERT ON INSTRUCTOR
  FOR EACH ROW
BEGIN
  SELECT instructor_sequence.nextval
  INTO :new.id
  FROM dual;
END;
/

instructionor_detail的顺序和触发器

CREATE SEQUENCE instructor_detail_sequence start with 1;

   CREATE OR REPLACE TRIGGER instructor_detail_on_insert
  BEFORE INSERT ON INSTRUCTOR_DETAIL
  FOR EACH ROW
BEGIN
  SELECT instructor_detail_sequence.nextval
  INTO :new.id
  FROM dual;
END;
/

讲师类如下:

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.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity
@Table(name="instructor")
public class Instructor {
 // annotate the class as an entity and map to db table

 // define the fields

 // annotate the fields with db column names

 // ** set up mapping to InstructorDetail entity

 // create constructors

 // generate getter/setter methods

 // generate toString() method
 @Id
 @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="INSTRUCTOR_SEQUENCE") @SequenceGenerator(name="INSTRUCTOR_SEQUENCE", sequenceName="INSTRUCTOR_SEQUENCE", allocationSize=1)

 @Column(name="id")
 private int id;

 @Column(name="first_name")
 private String firstName;

 @Column(name="last_name")
 private String lastName;
 @Column(name="email")
 private String email;

 @OneToOne(cascade=CascadeType.ALL)
 @JoinColumn(name="instructor_detail_id")
 private InstructorDetail instructorDetail;

 public Instructor() {

 }
 public Instructor(String firstName, String lastName, String email) {
 this.firstName = firstName;
 this.lastName = lastName;
 this.email = email;
 }
 public int getId() {
 return id;
 }
 public void setId(int id) {
 this.id = id;
 }
 public String getFirstName() {
 return firstName;
 }
 public void setFirstName(String firstName) {
 this.firstName = firstName;
 }
 public String getLastName() {
 return lastName;
 }
 public void setLastName(String lastName) {
 this.lastName = lastName;
 }
 public String getEmail() {
 return email;
 }
 public void setEmail(String email) {
 this.email = email;
 }
 public InstructorDetail getInstructorDetail() {
 return instructorDetail;
 }
 public void setInstructorDetail(InstructorDetail instructorDetail) {
 this.instructorDetail = instructorDetail;
 }
 @Override
 public String toString() {
 return "Instructor [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email
 + ", instructorDetail=" + instructorDetail + "]";
 }


}

类似于上述类,我的Instructor_detail类如下:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity
@Table(name="instructor_detail")
public class InstructorDetail {
 // annotate the class as an entity and map to db table

 // define the fields

 // annotate the fields with db column names

 // create constructors

 // generate getter/setter methods

 // generate toString() method

 @Id

 @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="INSTRUCTOR_DETAIL_SEQUENCE") @SequenceGenerator(name="INSTRUCTOR_DETAIL_SEQUENCE", sequenceName="INSTRUCTOR_DETAIL_SEQUENCE", allocationSize=1)
 @Column(name="id")
 private int id;

 @Column(name="youtube_channel")
 private String youtubeChannel;

 @Column(name="hobby")
 private String hobby;

 public InstructorDetail() {

 }
 public InstructorDetail(String youtubeChannel, String hobby) {
 this.youtubeChannel = youtubeChannel;
 this.hobby = hobby;
 }
 public int getId() {
 return id;
 }
 public void setId(int id) {
 this.id = id;
 }
 public String getYoutubeChannel() {
 return youtubeChannel;
 }
 public void setYoutubeChannel(String youtubeChannel) {
 this.youtubeChannel = youtubeChannel;
 }
 public String getHobby() {
 return hobby;
 }
 public void setHobby(String hobby) {
 this.hobby = hobby;
 }
 @Override
 public String toString() {
 return "InstructorDetail [id=" + id + ", youtubeChannel=" + youtubeChannel + ", hobby=" + hobby + "]";
 }

}

当我在以下主类中使用hibernate执行相同的操作

InstructorDetail tempInstructorDetail =
                    new InstructorDetail(
                            "http://www.youtube.com",
                            "Guitar");  

            Instructor tempInstructor = 
                    new Instructor("VILAS", "tADOORI", "vt@gmail.com");



            // associate the objects
            tempInstructor.setInstructorDetail(tempInstructorDetail);

            // start a transaction
            session.beginTransaction();

            // save the instructor
            //
            // Note: this will ALSO save the details object
            // because of CascadeType.ALL
            //
            System.out.println("Saving instructor: " + tempInstructor);
            session.save(tempInstructor);                   

            // commit transaction
            session.getTransaction().commit();

我收到以下关于ref完整性的错误消息。

INFO: HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
Saving instructor: Instructor [id=0, firstName=Madhu, lastName=Patel, email=madhu@luv2code.com, instructorDetail=InstructorDetail [id=0, youtubeChannel=http://www.youtube.com, hobby=Guitar]]
Hibernate: select INSTRUCTOR_SEQUENCE.nextval from dual
Hibernate: select INSTRUCTOR_DETAIL_SEQUENCE.nextval from dual
Hibernate: insert into instructor_detail (hobby, youtube_channel, id) values (?, ?, ?)
Hibernate: insert into instructor (email, first_name, instructor_detail_id, last_name, id) values (?, ?, ?, ?, ?)
Jul 11, 2018 6:10:40 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 2291, SQLState: 23000
Jul 11, 2018 6:10:40 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ORA-02291: integrity constraint (HBSTUDENT.FK_INSTRUCTOR) violated - parent key not found

Jul 11, 2018 6:10:40 PM org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release
INFO: HHH000010: On release of batch it still contained JDBC statements
Jul 11, 2018 6:10:40 PM org.hibernate.internal.ExceptionMapperStandardImpl mapManagedFlushFailure
ERROR: HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement]
Jul 11, 2018 6:10:40 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:oracle:thin:@localhost:1521/hbstudtracker]
Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:149)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:157)
    at org.

我已经在数据库上手动输入了值,它正在工作...我所做的任何不正确的事情,请告知。

问候 维拉斯

0 个答案:

没有答案