• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Insert new records in database without deleting existing records

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I need to know how can we insert new records into our table using hibernate. Actually when using hibernate query to insert record, it will delete the existing records and insert the new records. But i want to insert the new records along with the existing records.

Any information on this will be really helpful for me.



 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Actually when using hibernate query to insert record, it will delete the existing records and insert the new records



Is it so? I have used Hibernate queries and I have never had such problems. More details might help.
 
Durga Sanju
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wat i meant was when i close my application and launch it again, the insert query inserts only new set of records deleting the existing records
 
Durga Sanju
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
.save() query inserts every time only new records when i relaunch my application resulting in deletion of existing rows.

Thanks for explanation on this
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Durga,

Is the primary key the same? If so, you will over-write the existing records (if you are re-setting a counter, or something).
You may want to look into the annotation @GeneratedValue, an easy way to set up auto-incrementing in JPA.

-Shannon
 
Durga Sanju
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shannon,

I'm very new to hibernate. Can You please help me with an example for that.


Thanks,
-Durga
 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post your hibernate.cfg.xml. I think you use a wrong value for hbm2ddl-property.
 
Durga Sanju
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

I got the following exception

HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: /Save.jsp(3,5) Page directive must not have multiple occurrences of pageencoding
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:102)
org.apache.jasper.compiler.Validator$DirectiveVisitor.visit(Validator.java:194)
org.apache.jasper.compiler.Node$PageDirective.accept(Node.java:608)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2361)
org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2411)
org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2417)
org.apache.jasper.compiler.Node$Root.accept(Node.java:495)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2361)
org.apache.jasper.compiler.Validator.validateDirectives(Validator.java:1696)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:167)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:332)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:312)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:299)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:586)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)




i have included code of all the required files. Please tell wat's wrong with the code


//hibernate.cfg.xml


//Story.hbm.xml



//Writer.hbm.xml



//save.jsp


//Writer POJO class


//Story POJO Class


 
Durga Sanju
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found the error...

I included that page encoding directive twice in my JSP..

.

I got the following error in my JSP now...



type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: 11 in the generated java file
Only a type can be imported. org.hibernate.Transaction resolves to a package

An error occurred at line: 12 in the generated java file
Only a type can be imported. org.hibernate.Session resolves to a package

An error occurred at line: 13 in the generated java file
Only a type can be imported. org.hibernate.SessionFactory resolves to a package

An error occurred at line: 14 in the generated java file
Only a type can be imported. org.hibernate.HibernateException resolves to a package

An error occurred at line: 22 in the jsp file: /Save.jsp
Session cannot be resolved to a type
19: </head>
20: <body>
21: <%
22: Session sess=null;
23: SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
24: sess=sessionFactory.openSession();
25: System.out.println("Inserting Record");


An error occurred at line: 23 in the jsp file: /Save.jsp
SessionFactory cannot be resolved to a type
20: <body>
21: <%
22: Session sess=null;
23: SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
24: sess=sessionFactory.openSession();
25: System.out.println("Inserting Record");
26: Writer wr = new Writer();


An error occurred at line: 23 in the jsp file: /Save.jsp
Configuration cannot be resolved to a type
20: <body>
21: <%
22: Session sess=null;
23: SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
24: sess=sessionFactory.openSession();
25: System.out.println("Inserting Record");
26: Writer wr = new Writer();


An error occurred at line: 29 in the jsp file: /Save.jsp
The constructor Story(String) is undefined
26: Writer wr = new Writer();
27: wr.setName("Das");
28: ArrayList list = new ArrayList();
29: list.add(new Story("Story Name 1"));
30: list.add(new Story("Story Name 2"));
31: wr.setStories(list);
32: Transaction transaction = null;


An error occurred at line: 30 in the jsp file: /Save.jsp
The constructor Story(String) is undefined
27: wr.setName("Das");
28: ArrayList list = new ArrayList();
29: list.add(new Story("Story Name 1"));
30: list.add(new Story("Story Name 2"));
31: wr.setStories(list);
32: Transaction transaction = null;
33: try {


An error occurred at line: 32 in the jsp file: /Save.jsp
Transaction cannot be resolved to a type
29: list.add(new Story("Story Name 1"));
30: list.add(new Story("Story Name 2"));
31: wr.setStories(list);
32: Transaction transaction = null;
33: try {
34: transaction = sess.beginTransaction();
35: sess.save(wr);


Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:439)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:334)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:312)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:299)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:586)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)



i didnt change any code after that
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please show us the *complete* code of your Save.jsp. The errors that you are showing to us are related to the page directive, and the code that you have given doesn't have any page directive. Once your JSP is working we can move on to solving your Hibernate problem...
 
Durga Sanju
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

below is the jsp code....

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="java.sql.*"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.Iterator"%>
<%@page import="org.hibernate.Transaction"%>
<%@page import="org.hibernate.Session"%>
<%@page import="org.hibernate.SessionFactory"%>
<%@page import="org.hibernate.HibernateException"%>
<%@page import="org.hibernate.cfg.*" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
Session sess=null;
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
sess=sessionFactory.openSession();
System.out.println("Inserting Record");
Writer wr = new Writer();
wr.setName("Das");
ArrayList list = new ArrayList();
list.add(new Story("Story Name 1"));
list.add(new Story("Story Name 2"));
wr.setStories(list);
Transaction transaction = null;
try {
transaction = sess.beginTransaction();
sess.save(wr);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
throw e;
}
} finally {
sess.close();
}

%>
</body>
</html>

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
i have the same problem, and it seems hibernate creates a new table every time my app starts, i tried setting my hbm2ddl to just about everything, like: validate, update, create-drop, create, i even tryed to remove it from the config, but to no result, please help me.

thank you.
 
comanitza st.
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ooops, found my bug, and was a silly one indeed, i use ant to compile/build my files, and i touth that it will override my hibernate.cfg.xml, but it didnt, so i bassicly edited the one in my src folder, not in classes.
the good news it's that "hbm2ddl.auto" set to "update" works.

thank you.

comanitza
 
What I don't understand is how they changed the earth's orbit to fit the metric calendar. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic