0

I am trying to execute a basic hibernate application.However,I am consistently getting the error that is posted in the question.

Below posted is my project structure enter image description herecode:

Below is the code that is present in app.java

public class app {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    Session session =hibernate_utils.getSessionFactory().openSession();
    session.beginTransaction();
    contact  contact=new contact();
    contact.setFirstname("xxx");
    contact.setLastname("xxx");
    contact.setEmail("[email protected]");
    contact.setTelephone("xxxxxxxxxx");
    session.save(contact);
    session.getTransaction().commit();
    System.out.println("saved");
    }

  }

Below posted is the code that is present in the contact.java file

package net.rishanth.contact.form;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Column;
import static javax.persistence.GenerationType.SEQUENCE;

@Entity
@Table(name = "contacts")
public class contact {

@Id
@GeneratedValue(strategy=SEQUENCE)
@Column(name = "id", unique = true, nullable = false)     
public Integer getId() {
    return id;
}
public void setId(Integer id) {
    this.id = id;
}

@Column(name = "firstname", nullable = false)
public String getFirstname() {
    return firstname;
}
public void setFirstname(String firstname) {
    this.firstname = firstname;
}
@Column(name = "lastname", nullable = false)
 public String getLastname() {
    return lastname;
}
public void setLastname(String lastname) {
    this.lastname = lastname;
}
@Column(name = "email", nullable = false)
 public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}
@Column(name = "telephone", nullable = false)

public String getTelephone() {
    return telephone;
}
public void setTelephone(String telephone) {
    this.telephone = telephone;
}
private String firstname;
private String lastname;
private String email;
private String telephone;
private Integer id;

} 

Below posted is the code for my hiber_utils class present in service package.

package net.rishanth.contact.service;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class hibernate_utils {

private static final SessionFactory sessionfactory= buildSessionFatory();

@SuppressWarnings("deprecation")
private static SessionFactory buildSessionFatory(){
    // TODO Auto-generated method stub

        return new Configuration().configure().buildSessionFactory();



}
public static SessionFactory getSessionFactory()
{

    return sessionfactory;
}
public static void shutdown()
{

    getSessionFactory().close();
}

}

Below present is the hibernate.cnfg.xml file

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 <hibernate-configuration>
 <session-factory>
 <property    
 name="hibernate.bytecode.use_reflection_optimizer">false</property>
 <property   
 name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver
 </property>
 <property   
 name="hibernate.connection.url">jdbc:oracle:thin:@127.0.0.1:1521:XE
 </property>
 <property   name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect
 </property>
 <property name="hibernate.connection.username">system</property>
 <property name="hibernate.connection.password">xxxxx</property>
 <mapping class="net.rishanth.contact.form.contact"></mapping>
 </session-factory>
 </hibernate-configuration>

Below attached is my oracle screenshotenter image description here

Any help would be highly appreciated. Thanks!

3
  • 2
    Please post the error message instead of a screenshot of a part of the message. Anyways, the reason is quite obvious: the SEQUENCE is missing from the DB. Commented Jan 27, 2016 at 22:03
  • @MickMnemonic can you please tell me what sequence are you talking about? Commented Jan 27, 2016 at 22:04
  • It's really hard to know without seeing the full stack trace and knowing where the error originates. It might be the SEQUENCE used for generating Contact ids. Commented Jan 27, 2016 at 22:13

2 Answers 2

1

You have annotated the Contact entity use SEQUENCE as strategy. But you have not specified which sequence should be used. (I believe this is the error you might be getting. If not, posting the exception stack trace will help.)

In this case, by default hibernate looks for a sequence named hibernate_sequence and creating a sequence with this name should help.

Or, In case you want hibernate to use a sequence (say, your_sequence_name) that you have already created then further qualifying the @Id attribute as below should help:

@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="mySeq")
@GenericGenerator(name="mySeq", strategy="sequence", 
                parameters={
                        @Parameter(name="sequence_name", value="your_sequence_name")
                })
Sign up to request clarification or add additional context in comments.

Comments

0

Usually we need not to create the table/sequence in our DB, hibernate can manage the same.

For that you need to add below tag in your hibernate.cfg.xml

<property name="hbm2ddl.auto">create</property>

hbm2ddl.auto have few possible values:

  • create : When you create SessionFactory, hibernate drop everything(if exist) and create it again for you.

  • create-drop : create everything on start-up and drop them on shut-down

  • update : update the schema.

  • validate : validate the schema, makes no changes to the database.

Anyway, if you want to create table/sequence explicitly, you can create them.

But then while using @GeneratedValue you have to specify your sequence.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.