aspose file tools*
The moose likes Object Relational Mapping and the fly likes Insert new records in database without deleting existing records Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Insert new records in database without deleting existing records" Watch "Insert new records in database without deleting existing records" New topic
Author

Insert new records in database without deleting existing records

Durga Sanju
Greenhorn

Joined: Jun 03, 2010
Posts: 10
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.



Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
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

Joined: Jun 03, 2010
Posts: 10
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

Joined: Jun 03, 2010
Posts: 10
.save() query inserts every time only new records when i relaunch my application resulting in deletion of existing rows.

Thanks for explanation on this
Shannon McGee
Greenhorn

Joined: Oct 01, 2009
Posts: 18
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

Joined: Jun 03, 2010
Posts: 10
Hi Shannon,

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


Thanks,
-Durga
Christian Dillinger
Ranch Hand

Joined: Jul 20, 2009
Posts: 188
Please post your hibernate.cfg.xml. I think you use a wrong value for hbm2ddl-property.
Durga Sanju
Greenhorn

Joined: Jun 03, 2010
Posts: 10
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

Joined: Jun 03, 2010
Posts: 10
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
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Durga Sanju
Greenhorn

Joined: Jun 03, 2010
Posts: 10
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>
comanitza st.
Greenhorn

Joined: Jul 25, 2010
Posts: 2
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

Joined: Jul 25, 2010
Posts: 2
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Insert new records in database without deleting existing records
 
Similar Threads
Stuck on manyToMany association
insert 10,00000 records using hibernate with struts2
hybernate
Use of Hibernate to insert million records from Flat File
JPA Batch Insert