Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ec Kjellberg
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 346
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 346
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the answer (I prefer verbose answers)
/E
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic