aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes How to add unique index restriction with EJB/CMP/CMR? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "How to add unique index restriction with EJB/CMP/CMR?" Watch "How to add unique index restriction with EJB/CMP/CMR?" New topic
Author

How to add unique index restriction with EJB/CMP/CMR?

Ec Kjellberg
Greenhorn

Joined: Aug 12, 2004
Posts: 9
Newbie question:

Is there a way to add unique restriction to a column / field in your table / EJB other than the primary key?

Like you can do in SQL for example

CREATE TABLE COUNTRY (
COUNTRYID VARCHAR(10) NOT NULL,
COUNTRY_NAME VARCHAR(40) NOT NULL,
COUNTRY_PREFIX VARCHAR(40) NOT NULL,

PRIMARY KEY(COUNTRYID),
UNIQUE INDEX INAME(COUNTRY_NAME),
UNIQUE INDEX IPREFIX(COUNTRY_PREFIX)
);

Thx / Ec
Damanjit Kaur
Ranch Hand

Joined: Oct 18, 2004
Posts: 346
Hi,

Is there a way to add unique restriction to a column / field in your table / EJB other than the primary key?

Like you can do in SQL for example

CREATE TABLE COUNTRY (
COUNTRYID VARCHAR(10) NOT NULL,
COUNTRY_NAME VARCHAR(40) NOT NULL,
COUNTRY_PREFIX VARCHAR(40) NOT NULL,

PRIMARY KEY(COUNTRYID),
UNIQUE INDEX INAME(COUNTRY_NAME),
UNIQUE INDEX IPREFIX(COUNTRY_PREFIX)
);


In Ejb-QL for find methods with return type as java.util.Collection- the EJB-QL query can be like this

SELECT DISTINCT OBJECT(c.iname) FROM abstractschemaname AS c
Ec Kjellberg
Greenhorn

Joined: Aug 12, 2004
Posts: 9
Thanks for the answer but...

I'm not sure that it is exactly what I want.

I don't want to specify a find method to retrieve unique values of a field.

I'd like my EJB-server to throw a DuplicateKeyException if I try to create an EJB with the same value in my 'unique' field as another EJB already has!

I guess I could use that find method to check wether an EJB with that field value already exist and throw an Exception myself.

Is that what you usually do if you have the same 'problem'?
Damanjit Kaur
Ranch Hand

Joined: Oct 18, 2004
Posts: 346
I'd like my EJB-server to throw a DuplicateKeyException if I try to create an EJB with the same value in my 'unique' field as another EJB already has!

I guess I could use that find method to check wether an EJB with that field value already exist and throw an Exception myself.


O.K. for that you can use find method with return type as java.util.Collection and one argument for the value to to be searched. ejb-ql will be like this :

SELECT OBJECT(c.iname) FROM abstractschemaname AS c WHERE c.iname = ?1

On invoking find method you can check for number of elements in collection returned by this find method to determine for duplicate values and throw your exception.

Sorry, If I am too much descriptive, its just to make my steps more clear. I don't know of any other method.
Ec Kjellberg
Greenhorn

Joined: Aug 12, 2004
Posts: 9
Thanks for the answer (I prefer verbose answers)
/E
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How to add unique index restriction with EJB/CMP/CMR?