-->
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.  [ 3 posts ] 
Author Message
 Post subject: How to check out if object was already saved in database?
PostPosted: Sun Dec 11, 2005 2:01 pm 
Newbie

Joined: Sun Dec 11, 2005 1:51 pm
Posts: 4
Hi everybody,

This may be the easy question for many of you but not for me :)

I have some POJO class with an id attribute. For example

public class Continent {

private String continentName;

private Long id;
public Continent() {
}

public String getContinentName() {
return continentName;
}

public void setContinentName(String val) {
this.continentName = val;
}

public Long getId() {
return id;
}

private void setId(Long val) {
this.id = val;

}
}

Before I save this object into database, I want to check if object is already in database (saved). Is there any easy way of getting this information?

It depends on that information if object is to be saved or not, because I want to obtain an UNIQUE constraint in my database.

Please help me

Regards


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 11, 2005 2:07 pm 
Beginner
Beginner

Joined: Fri Jul 22, 2005 3:35 pm
Posts: 24
Location: Buenos Aires, Argentina
You could implement a getContinent(String c) which would perform a query ("from Continent where continentName=?"). If there's an instance it would return it and if it's not it would create it and save it into the session. Then you would make Continent constructor non-public (ie. package private, you can't make it private).


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 12, 2005 9:07 am 
Beginner
Beginner

Joined: Mon May 02, 2005 6:17 pm
Posts: 41
Well if you have for assigned class some generator, sequence or identity you could check if it's id is null or not. So if it's null it' not saved. If it' not null , then it's probably saved but you could try to load it using that id just to be sure.
Pozdrav iz Srbije!

_________________
www.globalresearch.ca


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