Soy nuevo en el aprendizaje de Hibernate, así que estudio las relaciones que se utilizan en él. Lo que he entendido es: para el mapeo de relación unidireccional OneToMany con clave externa, la columna de unión estaría en la entidad objetivo (en mi caso, clase de revisión). Y no necesito hacer que sea bidireccional para que funcione.

Pero al implementarlo, obtengo el siguiente error:

Hibernate: insert into course (title, id) values (?, ?)
Hibernate: insert into review (comment, id) values (?, ?)
2020-05-07 19:44:24 WARN  SqlExceptionHelper:137 - SQL Error: 928, SQLState: 42000
2020-05-07 19:44:24 ERROR SqlExceptionHelper:142 - ORA-00928: missing SELECT keyword

Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute statement
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188)
    at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1356)
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:443)
    at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3202)
    at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2370)
    at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:447)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:183)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$300(JdbcResourceLocalTransactionCoordinatorImpl.java:40)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:281)
    at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:101)
    at com.practice.hibernate008.onetomany.unidirectional.HibernateApp_1_CreateCourseAndReviewsDemo.main(HibernateApp_1_CreateCourseAndReviewsDemo.java:40)

Clase de entidad del curso:

@Entity
@Table(name="course")
public class Course {

    @Id
    @SequenceGenerator(allocationSize=1,name="id_generator", sequenceName="ID_GENERATOR_SEQ",initialValue=30)
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="id_generator")   //Auto Increment feature - Strategy to handle multiple objects
    @Column(name="id")
    private int id;

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

    @OneToMany(cascade=CascadeType.ALL)
    @JoinColumn(name="course_id")
    private List<Review> reviews;

    public Course() {}

    public Course(String title) {
        super();
        this.title = title;
    }

    //add convenience method
    public void addReview(Review review){
        if(reviews == null){
            reviews = new ArrayList<Review>();
        }
        reviews.add(review);
    }

    public int getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public List<Review> getReviews() {
        return reviews;
    }

    public void setReviews(List<Review> reviews) {
        this.reviews = reviews;
    }

    @Override
    public String toString() {
        return "Course [id=" + id + ", title=" + title + ", reviews=" + reviews + "]";
    }
}

Clase de entidad de revisión:

@Entity
@Table(name="review")
public class Review {

    @Id
    @SequenceGenerator(allocationSize=1,name="id_generator", sequenceName="ID_GENERATOR_SEQ",initialValue=30)
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="id_generator")
    @Column(name="id")
    private int id;

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

    public Review() {}

    public Review(String comment) {
        super();
        this.comment = comment;
    }

    public int getId() {
        return id;
    }

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

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    @Override
    public String toString() {
        return "Review [id=" + id + ", comment=" + comment + "]";
    }

}

Método principal:

public static void main(String[] args) {

    SessionFactory sessionFactory = new Configuration().configure("hibernate-configuration.xml")
                                                       .addAnnotatedClass(Course.class)
                                                       .addAnnotatedClass(Review.class)
                                                       .buildSessionFactory();

    Session session = sessionFactory.getCurrentSession();

    try{

        Course course = new Course("Hibernate - Beginner Course");
        course.addReview(new Review("Great Course ... loved it."));
        course.addReview(new Review("Cool Course ... well done!!"));
        course.addReview(new Review("Dump Course .. You are an idiot."));

        session.beginTransaction();
        System.out.println("Saving the course :: " + course);
        session.save(course);

        session.getTransaction().commit();
    } finally{
        sessionFactory.close();
    }
}

SQL para la tabla creada:

CREATE TABLE "COURSE" 
(   
    "ID" NUMBER NOT NULL ENABLE, 
    "TITLE" VARCHAR2(50 BYTE) DEFAULT null, 
     PRIMARY KEY ("ID"),
 );

CREATE TABLE "REVIEW" 
(   
    "ID" NUMBER NOT NULL ENABLE, 
    "COMMENT" VARCHAR2(256 BYTE) DEFAULT null, 
    "COURSE_ID" NUMBER  DEFAULT NULL, 
     PRIMARY KEY ("ID"),
     FOREIGN KEY("COURSE_ID") REFERENCES "COURSE"("ID")
);

Link de referencia :

OneToMany-UniDirectional-Example3

Example 3: Unidirectional One-to-Many association using a foreign key mapping

    // In Customer class:

    @OneToMany(orphanRemoval=true)
    @JoinColumn(name="CUST_ID") // join column is in table for Order
    public Set<Order> getOrders() {return orders;}

Configuración de hibernación:

<hibernate-configuration>
    <session-factory>
        <!-- JDBC Database connection settings -->
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:oracledb</property>
        <property name="connection.username">hibernate</property>
        <property name="connection.password">hibernate</property>

        <!-- JDBC connection pool settings ... using built-in test pool -->
        <property name="connection.pool_size">1</property>

        <!-- Select our SQL dialect -->
        <property name="dialect">org.hibernate.dialect.OracleDialect</property>

        <!-- Echo the SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Set the current session context -->
        <property name="current_session_context_class">thread</property>
    </session-factory>
</hibernate-configuration>

Intenté las siguientes opciones pero no funcionó:

 1. escaping character backtick ("`") 
 2. escaping character (""\") 
 3. hibernate property : <property name="hibernate.globally_quoted_identifiers">true</property>

Por favor ayuda para esto !!!

1
Tarun Verma 7 may. 2020 a las 17:42

2 respuestas

La mejor respuesta

Finalmente, logré resolverlo cambiando el nombre de la columna en DB a course_comment

@Column(name="course_comment")
private String courseComment;
0
Tarun Verma 15 may. 2020 a las 12:03

En realidad, tu mapeo está bien. Solo debe agregar backticks para el palabra clave reservada del oráculo {{X0} }.

@Entity
@Table(name="review")
public class Review {

   @Column(name = "`comment`")
   private String comment;

   // ...
}
0
SternK 7 may. 2020 a las 17:34