-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 14 posts ] 
Author Message
 Post subject: How to insert 'date' into mysql by using Hibernate
PostPosted: Tue Oct 14, 2008 12:35 pm 
Newbie

Joined: Thu Oct 02, 2008 10:43 am
Posts: 11
I am using Hibernate with mysql

I have one field 'date'

My code


public Date getDate() {

return date;
}

public void setDate(Date date) {

this.date = date;

}

My mapping file is

<property name="date" column="date"/>


I have table with field 'Date'in mysql


when I insert data it is not inserted into mysql

mysql default format is 'yyyy-mm-dd'

Please help me how to solve the problem? Can I enter the date as
'dd-mm-yyyy' and store same format in mysql.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 14, 2008 2:02 pm 
Newbie

Joined: Sun Oct 12, 2008 9:17 am
Posts: 9
have you try this?

<property name="date" type="date">
<column name="date"/>
</property>

I work with dates in mysql 5 and it works.

I hope it will be helpfull


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 14, 2008 6:23 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I maybe missing something but I don't understand your problem. I don't understand why the date format should have anything to do with this. Your code is using a Date object, not a String.

The code and mapping you are showing are clearly only minor parts of your project. Can you show us more of the code that you are having a problem with.

The simplest way convert between a String and Date is to use a java.text.SimpleDateFormat object.

Code:
SimpleDateFormat format = new SimpleDateFormat("dd-mm-yyyy");
String dateString = "15-10-2008";
Date date = format.parse(dateString);
System.out.println(format.format(date));


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 15, 2008 5:21 am 
Newbie

Joined: Thu Oct 02, 2008 10:43 am
Posts: 11
This is not working

I have jsp form with fields name,quantity,date. when I enter datas that should be inserted into mysql table.

I am facing problem with date.

I have confusion. I want to retrieve record as startingdate---enddate after sometime. For this I have to use Date object or String.

Please explain me

My code

import java.util.Date;
public class Example {

int userId;
String name;
int quantity;
Date creationDate;


/**
* @return Returns the Name.
*/
public String getName() {
return name;
}
/**
* @param stockCode The stockCode to set.
*/
public void setName(String name) {
this.name = name;
}

/**
* @return Returns the quantity.
*/
public int getQuantity() {
return quantity;
}
/**
* @param quantity The quantity to set.
*/
public void setQuantity(int quantity) {
this.quantity = quantity;
}

/**
* @return Returns the userId.
*/
public int getUserId() {
return userId;
}
/**
* @param userId The userId to set.
*/
public void setUserId(int userId) {
this.userId = userId;
}

/**
* @return Returns the userId.
*/
public Date getCreationDate() {

return date;
}
/**
* @param userId The userId to set.
*/
public void CreationDate(Date date) {


this.date = date;

}


}


Mapping file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="Example" table="Example">
<id name="userId" column="USER_ID" type="int">
<generator class="native"/>
</id>

<property name="name" column="name"/>
<property name="quantity" column="quantity"/>
<property name="creationDate" column="date"/>




</class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 15, 2008 6:00 am 
Beginner
Beginner

Joined: Fri May 14, 2004 9:50 am
Posts: 28
You should put the type of the column in the mapping :

type="date"

What is the exception thrown by hibernate ?

_________________
Eric

http://www.viaxoft.com
http://blog.viaxoft.net


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 15, 2008 6:33 am 
Newbie

Joined: Thu Oct 02, 2008 10:43 am
Posts: 11
I am not getting any exception. But the values are not inserted


Top
 Profile  
 
 Post subject: Re: How to insert 'date' into mysql by using Hibernate
PostPosted: Wed Oct 15, 2008 6:55 am 
Newbie

Joined: Wed Oct 15, 2008 6:50 am
Posts: 4
mahest2001 wrote:
I am using Hibernate with mysql

I have one field 'date'

My code


public Date getDate() {

return date;
}

public void setDate(Date date) {

this.date = date;

}

My mapping file is

<property name="date" column="date"/>


I have table with field 'Date'in mysql


when I insert data it is not inserted into mysql

mysql default format is 'yyyy-mm-dd'

Please help me how to solve the problem? Can I enter the date as
'dd-mm-yyyy' and store same format in mysql.



try "dates" or "sthdate" instead. date is a word that mysql keepd.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 15, 2008 12:24 pm 
Newbie

Joined: Thu Oct 02, 2008 10:43 am
Posts: 11
I tried. It is not working. Please help me What is the problem ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 15, 2008 1:22 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
I am not getting any exception. But the values are not inserted


Can you post the code that is doing the insert? You are saying that you have a JSP form. Where does the information entered into that form go? Where are creating the Example object and setting the properties on it? Where are you calling Session.save() to tell Hibernate to store it in the database?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 15, 2008 1:33 pm 
Newbie

Joined: Thu Oct 02, 2008 10:43 am
Posts: 11
This is my controller

import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletException;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class ExampleController extends SimpleFormController {
protected ExampleDAO exampledao;


public Object formBackingObject(HttpServletRequest request) throws ServletException
{
Example backingObject = new Example();

return backingObject;
}


public ModelAndView onSubmit(Object command) throws ServletException {

Example example = (Example)command;

getExampledao().save(example);
return new ModelAndView("success");

}


public ExampleDAO getExampledao() {
return exampledao;
}

public void setExampledao(ExampleDAO exampledao) {
this.exampledao = exampledao;
}

}



import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import org.springframework.orm.hibernate.support.HibernateDaoSupport;


public class ExampleDAO {

private SessionFactory sessionFactory;
public void save(Example example){
Session session = getSessionFactory().openSession();
try
{
Transaction tx = session.beginTransaction();
session.save(example);
tx.commit();
}catch(Exception e){
e.printStackTrace();
}finally{
session.close();
}

}


/**
* @return Returns the sessionFactory.
*/
public SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* @param sessionFactory The sessionFactory to set.
*/
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 15, 2008 2:13 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I am not very familiar with the Spring framework, but from your code I assume that spring is using the formBackingObject() and onSubmit() method to interact with your code. The Example object returned from the formBackingObject() is clearly an empty object. Is it supposed to be populated with data (by Spring) before the onSubmit() method is called? Have you checked this with simple debug output in the onSubmit() method?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 16, 2008 5:23 am 
Newbie

Joined: Thu Oct 02, 2008 10:43 am
Posts: 11
The problem is with Date object.

If I use String object instead of Date the values are inserted

public class Example {

String creationDate;

public String getcreationDate() {

return creationDate;
}
public void setcreationDate(String creationDate) {

this.creationDate = creationDate;
}
}

When I use Date, the values are not inserted. I have confusion whether I have to use String or Date. I want to retrieve records StartDate-->EndDate from database after sometime. Please explain me how the datas are converted and which type should be stored in database


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 16, 2008 6:15 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I think this is a Spring problem. Somehow you have to tell Spring to convert the string from the form to a Date object. I don't know how to do that since I am not using Spring myself. I found plenty of documents when searching on Google that seems related to this subject. For example, this may help you: http://robertmarkbramprogrammer.blogspo ... g-mvc.html


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 16, 2008 7:24 am 
Newbie

Joined: Thu Oct 02, 2008 10:43 am
Posts: 11
Thank you very much nordborg.

This thread helps me
http://forum.springframework.org/archiv ... 10243.html

It is working now
Thank you for your help


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 14 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.