Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Hibernate hibernate annotations
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


suresh anugandula
hibernate annotations
0 Answer(s)      3 years and 6 months ago
Posted in : Hibernate


I am facing following problem, I have created 2 tables (student and address) with foreign key in address table. I am using hibernate annotations to insert records into these tables. But it is trying to insert records into 'student_tbl_address_tbl' which is not created in database.

Following ar pojo classes

  - Student.java

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="student_tbl")

public class Student implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 8876327743522818732L;


    @Id
    private long sid;
    @Column(name="sname")
    private String sname;
    @Column(name="age")
    private double age;
    @OneToMany(cascade=CascadeType.ALL)
    private Set<Address> addresses=new HashSet<Address>(0);


    @OneToMany(mappedBy="student",cascade=CascadeType.ALL)
    public Set<Address> getAddresses() {
        return addresses;
    }
    public void setAddresses(Set<Address> addresses) {
        this.addresses = addresses;
    }
    public long getSid() {
        return sid;
    }
    public void setSid(long sid) {
        this.sid = sid;
    }
    public String getSname() {
        return sname;
    }
    public void setSname(String sname) {
        this.sname = sname;
    }

    public double getAge() {
        return age;
    }
    public void setAge(double age) {
        this.age = age;
    }
}

  - Address.java

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name="address_tbl")
public class Address implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 6583374367093492573L;
    @Id
    long adno;
    @Column(name="street")
    String street;
    @Column(name="city")
    String city;
    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="sid")
    Student student;
    public long getadno() {
        return adno;
    }
    public void setadno(long adno) {
        this.adno = adno;
    }
    public String getStreet() {
        return street;
    }
    public void setStreet(String street) {
        this.street = street;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public Student getStudent() {
        return student;
    }
    public void setStudent(Student student) {
        this.student = student;
    }


}

I have created following class to store the records inside the database

  - Write.java

import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class Write {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Session session=HibernateSessionFactory.getSession();
        Transaction tx=session.beginTransaction();

        Student s=new Student();
        s.setAge(25d);
        s.setSid(1l);
        s.setSname("suresh");

        Set<Address> addressSet=new HashSet<Address>();
        Address a=new Address();
        a.setadno(1l);
        a.setCity("hyd");
        a.setStreet("kukatpally");
        a.setStudent(s);
        addressSet.add(a);

        a=new Address();
        a.setadno(2l);
        a.setCity("sec");
        a.setStreet("ameerpet");
        a.setStudent(s);

        addressSet.add(a);
        s.setAddresses(addressSet);
        session.save(s);

        tx.commit();

        session.close();

    }

}

following is the exception i got,

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select address_.adno, address_.city as city1_, address_.street as street1_, address_.sid as sid1_ from address_tbl address_ where address_.adno=?
Hibernate: select address_.adno, address_.city as city1_, address_.street as street1_, address_.sid as sid1_ from address_tbl address_ where address_.adno=?
Hibernate: insert into student_tbl (age, sname, sid) values (?, ?, ?)
Hibernate: insert into address_tbl (city, street, sid, adno) values (?, ?, ?, ?)
Hibernate: insert into address_tbl (city, street, sid, adno) values (?, ?, ?, ?)
Hibernate: insert into student_tbl_address_tbl (student_tbl_sid, addresses_adno) values (?, ?)
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not insert collection: [com.tbss.Student.addresses#1]
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1205)
    at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:58)
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:171)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:366)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
    at com.tbss.Write.main(Write.java:41)
Caused by: java.sql.SQLException: Invalid object name 'student_tbl_address_tbl'.
    at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:368)
    at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2820)
    at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2258)
    at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:632)
    at net.sourceforge.jtds.jdbc.JtdsStatement.processResults(JtdsStatement.java:584)
    at net.sourceforge.jtds.jdbc.JtdsStatement.executeSQL(JtdsStatement.java:546)
    at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeUpdate(JtdsPreparedStatement.java:504)
    at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:46)
    at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1168)
    ... 10 more

Please send the answer to "Anugandula.Suresh@Tata-bss.com"

Advertisement
View Answers

Related Tutorials/Questions & Answers:
Hibernate Annotations ManytoMany - Hibernate
Hibernate Annotations ManytoMany  Can anybody tell me how to have a many to many relationship through Hibernate Annotations not through the Hbm.xml files plz
hibernate annotations
hibernate annotations to insert records into these tables. But it is trying...hibernate annotations  I am facing following problem, I have created... system properly. Hibernate: select address_.adno, address_.city as city1
Advertisements
hibernate annotations example for mappings
hibernate annotations example for mappings  Please send me example of hibernate annotations for mappings.Please send the example   Please go through the following link: Hibernate Annotations
Hibernate 4 annotations tutorial
Hibernate 4 annotations tutorial  Hi, I am trying to find the best Hibernate 4 annotations tutorial. I think there is good tutorial on your site. Please tell me the url of Hibernate 4 annotations tutorial. Thanks   hi
Hibernate Annotations
Hibernate Annotations In this section we will learn about Hibernate Annotations and understand how Hibernate Annotations is simplifying the programming... as Hibernate annotations and it based on the Java 5 annotations. So, to use Hibernate
Spring Hibernate Annotations
In this section, you will learn about the common annotations used for Spring Hibernate Integration
Hibernate mapping annotations
In this section, you will learn about the annotations used for various mapping in Hibernate
Hibernate 4 Annotations
Hibernate 4 Annotations In this tutorial you will learn about the use of annotations in Hibernate. To create an example in Hibernate using Annotation reader... and do process as it is defined for. Hibernate also needs to provide
Hibernate Annotations
Hibernate Annotations       You have... Annotations part.  Download Source Code Hibernate Annotations: Hibernate needs... and does the processing accordingly. The Hibernate Annotations is the powerful
Unable to create annotations in hibernate 4.0
Unable to create annotations in hibernate 4.0  I worked on a similar example as yours.But when I create a new hibernate console configuration... is deprecated in hibernate 4.0 ,hence I used Configuration object as shown by you. Kindly
Hibernate Annotations
This tutorial describes the concept of annotations
hibernate annotations with composite primary key
hibernate annotations with composite primary key  I want to know how to use hibernate annotations in case of composite primary key in one table. I... the following link: Hibernate Notations
Hibernate Validator Annotations
In this section, you will learn how to validate in Hibernate using validator annotations in bean class
Hibernate Annotations Example
Hibernate Annotations Example In this section we will see how we can develop Hibernate Annotations based example program. After completing this tutorial you will be able to use Hibernate Annotations in your project and develop
Hibernate One to Many Self Join using Annotations
In this section, you will learn one to many self join using Annotations in Hibernate
Hibernate Annotations
Hibernate Annotations       Hibernate Annotations examples and tutorials. Quick Hibernate... accordingly. The Hibernate Annotations is the powerful way to provide the metadata
Hibernate 4 Annotations Tutorial
Video Tutorial: Hibernate 4 Annotations Tutorial In this video tutorial I will explain you the steps needed to create example program using Annotations... such applications. We have used the Eclipse IDE. What is Hibernate Annotations
Hibernate Many to Many Self Join using Annotations
In this section, you will learn how to do many to many self join using Annotations in Hibernate
Annotations
Annotations       Sun... efficient in jdk 5. The main objective to develop the annotations is to make the development easier. Annotations behaves like the meta. The literal meaning
Annotations
Annotations       Annotations in computer programming languages provide data about a program... directly to the operation of the code to which they annotate. Annotations
Annotations
Annotations       Sun... efficient in jdk 5. The main objective to develop the annotations is to make the development easier. Annotations behaves like the meta. The literal meaning
Annotations are using now a days?
Annotations are using now a days?  i have a doubt in Annatation, if we use Annatation we must hardcoding the code/data but sunmicrosystem itself says that hardcoding is not recommended
Hibernate Configuration
The configuration of Hibernate is handled by the instance of org.hibernate.cfg.Configuration . It symbolizes the complete mapping between Java data types... org.hibernate.SessionFactory . Various XML mapping files or Java Annotations compilation build
Annotations in Java
Annotations behave like metadata and provide data about a program... the annotated elements. Developing Annotations helps in faster development of program. Use of Annotations: Compiler can use Annotations to detect errors
hibernate
why hibernate?  why hibernate?   Hibernate: -Hibernate... library. It solves object-relational impedance mismatch problems. Hibernate makes the application development process easy Hibernate saves the development
Hibernate 3 Query Example Part 2
Hibernate 3 Query Example Part 2       Hibernate 3 Query Example Part 2 This tutorial  explains  how to create  Hibernate 3 query example with Spring 3 
Spring 3 MVC and Hibernate 3 Example Part 1
Spring 3 MVC and Hibernate 3 Example application using Annotations This tutorial explains how to use annotations with spring 3 MVC and hibernate 3 based application to make the development easier and faster than ever before. You can
Hibernate Annotattions - Hibernate
hibernate annotations which is a new facility in hibernate 3.2 any tutorial and what.... The Hibernate Annotations is the powerful way to provide the metadata for the Object.../hibernateannotations/hibernate-annotations-tutorial.shtml http
JDBC4.0-Dataset implementation of SQL using Annotations
JDBC4.0-Dataset implementation of SQL using Annotations An annotation..., are used to inject code at runtime.The Annotations allows developers.... There are two main annotations when specifying SQL queries in Java code
HIBERNATE
HIBERNATE   What is difference between Jdbc and Hibernate
Advertisements
 

 

 

DMCA.com