-->
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.  [ 11 posts ] 
Author Message
 Post subject: Problem with Inserting to Oracle
PostPosted: Fri Dec 19, 2008 3:45 am 
Newbie

Joined: Thu Dec 18, 2008 3:09 am
Posts: 11
Location: Visakhapatnam
I am struggling to insert new values into a Oracle 10g table which has a primary key (Id) which is an integer and It is a sequence column. I created sequence(Test_seq) and Trigger in that table.
In Hibernate Mapping files

<id name="ID" unsaved-value="0">
<generator class="sequence">
<param name="sequence">Test_seq</param>
</generator>


In .NET User Interface, I am Inserting a new row to GridView, the Row is not Inserting. I debug the program all values are shown , but Not inserting. Some times one record is inserting after that The records are not inserting. Update and Delete functions are working. But Inserting is Problem.

Please help me...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 19, 2008 6:00 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
For what is the trigger ? Hibernate should use the sequence in the insert statement. So there's no need for a trigger. But I might get that wrong.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Doubt
PostPosted: Fri Dec 19, 2008 8:01 am 
Newbie

Joined: Thu Dec 18, 2008 3:09 am
Posts: 11
Location: Visakhapatnam
Sir,

My Company using ntier architecture. In Data Access Layer I am using
session.SaveOrUpdateCopy(newTest); So How to use the sequence in the insert statement. Please explain with small example sir.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 19, 2008 8:06 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
As far as I know hibernate internally uses the sequence when it creates the sql statement. So you do not have to do anything in your application.

You have to specify the sequence in the mapping, that's it:

<id name="ID" unsaved-value="0">
<generator class="sequence">
<param name="sequence">Test_seq</param>
</generator>

Hibernate then will build somethin like this:

insert into xyz (....) values (... Test_seq.NextVal() ...)

Enable "show_sql=true" and check the log. There you can see what I'm talking about.

If you additionally have an trigger that sets the id, you will run into problems with hibernate.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 20, 2008 7:31 am 
Newbie

Joined: Thu Dec 18, 2008 3:09 am
Posts: 11
Location: Visakhapatnam
I droped the Trigger and I try to insert the new record. But, the record is not inserted.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 20, 2008 9:11 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Any exceptions ? Set log level to debug and check the output. And post the code where you save the object.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 23, 2008 6:22 am 
Newbie

Joined: Thu Dec 18, 2008 3:09 am
Posts: 11
Location: Visakhapatnam
I am sending the Domain Object, Mapping Document, Data Access Layer Save() Method, and User Interface Code.

Domain Objects are:
public class Testplan
{
int _TestPlanID;

public int TestPlanID
{
get { return _TestPlanID; }
set { _TestPlanID = value; }
}
string _TestPlan;

public string TestPlan
{
get { return _TestPlan; }
set { _TestPlan = value; }
}
string _Description;

public string Description
{
get { return _Description; }
set { _Description = value; }
}

string _Active_Ind;

public string Active_Ind
{
get { return _Active_Ind; }
set { _Active_Ind = value; }
}

int _CreatedBy;

public int CreatedBy
{
get { return _CreatedBy; }
set { _CreatedBy = value; }
}
DateTime _CreatedDate;

public DateTime CreatedDate
{
get { return _CreatedDate; }
set { _CreatedDate = value; }
}
int _UpdatedBy;

public int UpdatedBy
{
get { return _UpdatedBy; }
set { _UpdatedBy = value; }
}
DateTime _UpdatedDate;

public DateTime UpdatedDate
{
get { return _UpdatedDate; }
set { _UpdatedDate = value; }
}
string _Disabled;

public string Disabled
{
get { return _Disabled; }
set { _Disabled = value; }
}

}
Mapping document:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Dev.Medho.TestEnh.Kasyapa.DTO"
namespace="Dev.Medho.TestEnh.Kasyapa.DTO.Domain.Screens">
<class name="Testplan" table="TestPlan" lazy="false">
<id name="TestPlanID" unsaved-value="0">
<generator class="sequence">
<param name="sequence">TestPlan_seq</param>
</generator>
</id>
<property name="TestPlan" />
<property name="Description" />
<property name="Active_Ind" />
<property name="CreatedBy" />
<property name="CreatedDate"/>
<property name="UpdatedBy" />
<property name="UpdatedDate" />
<property name="Disabled" />
</class>
</hibernate-mapping>

In Data Access Layer:

public void Save(Testplan newTestPlan)
{
if (log.IsDebugEnabled) log.Debug("Entered Save method");

try
{
session.SaveOrUpdateCopy(newTestPlan);
}
catch (Exception ex)
{
if (log.IsErrorEnabled)
log.Error("Cannot SaveTestplan", ex);
}
if (log.IsDebugEnabled) log.Debug("Leave Save Method");
}

In User Interface:
protected void btnInsert_Click(object sender, EventArgs e)
{
Testplan newTestPlan = new Testplan();
newTestPlan.TestPlan = txtTestPlan.Text;
newTestPlan.Description = txtDescription.Text;
newTestPlan.Active_Ind = "T";
newTestPlan.CreatedBy = 1;
newTestPlan.CreatedDate = DateTime.Today;
newTestPlan.UpdatedBy = 1;
newTestPlan.UpdatedDate = DateTime.Today;
newTestPlan.Disabled = "T";
testPlanBL.SaveTestPlanDetails(newTestPlan);
}

Please verify the code and suggest where I have wrong.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 23, 2008 6:29 am 
Newbie

Joined: Thu Dec 18, 2008 3:09 am
Posts: 11
Location: Visakhapatnam
I am sending the Domain Object, Mapping Document, Data Access Layer Save() Method, and User Interface Code.

Domain Objects are:
public class Testplan
{
int _TestPlanID;

public int TestPlanID
{
get { return _TestPlanID; }
set { _TestPlanID = value; }
}
string _TestPlan;

public string TestPlan
{
get { return _TestPlan; }
set { _TestPlan = value; }
}
string _Description;

public string Description
{
get { return _Description; }
set { _Description = value; }
}

string _Active_Ind;

public string Active_Ind
{
get { return _Active_Ind; }
set { _Active_Ind = value; }
}

int _CreatedBy;

public int CreatedBy
{
get { return _CreatedBy; }
set { _CreatedBy = value; }
}
DateTime _CreatedDate;

public DateTime CreatedDate
{
get { return _CreatedDate; }
set { _CreatedDate = value; }
}
int _UpdatedBy;

public int UpdatedBy
{
get { return _UpdatedBy; }
set { _UpdatedBy = value; }
}
DateTime _UpdatedDate;

public DateTime UpdatedDate
{
get { return _UpdatedDate; }
set { _UpdatedDate = value; }
}
string _Disabled;

public string Disabled
{
get { return _Disabled; }
set { _Disabled = value; }
}

}
Mapping document:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Dev.Medho.TestEnh.Kasyapa.DTO"
namespace="Dev.Medho.TestEnh.Kasyapa.DTO.Domain.Screens">
<class name="Testplan" table="TestPlan" lazy="false">
<id name="TestPlanID" unsaved-value="0">
<generator class="sequence">
<param name="sequence">TestPlan_seq</param>
</generator>
</id>
<property name="TestPlan" />
<property name="Description" />
<property name="Active_Ind" />
<property name="CreatedBy" />
<property name="CreatedDate"/>
<property name="UpdatedBy" />
<property name="UpdatedDate" />
<property name="Disabled" />
</class>
</hibernate-mapping>

In Data Access Layer:

public void Save(Testplan newTestPlan)
{
if (log.IsDebugEnabled) log.Debug("Entered Save method");

try
{
session.SaveOrUpdateCopy(newTestPlan);
}
catch (Exception ex)
{
if (log.IsErrorEnabled)
log.Error("Cannot SaveTestplan", ex);
}
if (log.IsDebugEnabled) log.Debug("Leave Save Method");
}

In User Interface:
protected void btnInsert_Click(object sender, EventArgs e)
{
Testplan newTestPlan = new Testplan();
newTestPlan.TestPlan = txtTestPlan.Text;
newTestPlan.Description = txtDescription.Text;
newTestPlan.Active_Ind = "T";
newTestPlan.CreatedBy = 1;
newTestPlan.CreatedDate = DateTime.Today;
newTestPlan.UpdatedBy = 1;
newTestPlan.UpdatedDate = DateTime.Today;
newTestPlan.Disabled = "T";
testPlanBL.SaveTestPlanDetails(newTestPlan);
}

Please verify the code and suggest where I have wrong.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 23, 2008 9:58 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
SaveOrUpdateCopy is not enough. You also have to flush the session or commit the surrounding transaction (if you have one).

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 24, 2008 12:33 am 
Newbie

Joined: Thu Dec 18, 2008 3:09 am
Posts: 11
Location: Visakhapatnam
I am using session.Flush(), Insert functionality is working sir. I struggled 10 days for this problem. Thanks for helping me.
Thank you agin.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 24, 2008 5:34 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
I suggest a book about nhibernate or hibernate. Will save you a lot of pain in the future.

_________________
--Wolfgang


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 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.