-->
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.  [ 2 posts ] 
Author Message
 Post subject: getConstraintName returns null
PostPosted: Sat Aug 12, 2006 6:31 pm 
Newbie

Joined: Mon Jul 31, 2006 10:57 am
Posts: 8
Hi,

I'm using Hibernate annotations together with the database PostgreSQL 8.1.2.

In some classes I'm using unique Constraints

@Entity
public class ClubFunctionType extends PersistentObject {

@Column(unique=true)
private String name;

....
}

If the constraint is violated Hibernate throws (as expected) an ConstraintViolationException. But the method getConstraintName () returns null.

If I violate the same constraint direct in the database I get:

test=# insert into clubfunctiontype values (5,'SaveVorsitzender','');
ERROR: duplicate key violates unique constraint "clubfunctiontype_name_key"

Is there a possibilty to get a part of this error message as return value of the getConstraintName method?

Thanks for help!


Top
 Profile  
 
 Post subject: Re: getConstraintName returns null
PostPosted: Wed Oct 11, 2006 7:08 am 
Newbie

Joined: Wed May 17, 2006 8:26 am
Posts: 8
Location: Amsterdam
ThorstenFreie wrote:
Hi,
If the constraint is violated Hibernate throws (as expected) an ConstraintViolationException. But the method getConstraintName () returns null.

If I violate the same constraint direct in the database I get:

test=# insert into clubfunctiontype values (5,'SaveVorsitzender','');
ERROR: duplicate key violates unique constraint "clubfunctiontype_name_key"

Is there a possibilty to get a part of this error message as return value of the getConstraintName method?


Yes, there is. You'll need to either subclass or modify a method in org.hibernate.dialect.PostgreSQLDialect. Of interest to you will be the getViolatedConstraintNameExtracter method. At the moment there's no logic there to detect duplicate key referential integrity violations:

Code:
public ViolatedConstraintNameExtracter getViolatedConstraintNameExtracter() {
    return EXTRACTER;
}

private static ViolatedConstraintNameExtracter EXTRACTER = new TemplatedViolatedConstraintNameExtracter() {
    public String extractConstraintName(SQLException sqle) {
        try {
            int sqlState = Integer.valueOf( JDBCExceptionHelper.extractSqlState(sqle)).intValue();
            switch (sqlState) {
                // CHECK VIOLATION
                case 23514: return extractUsingTemplate("violates check constraint "",""", sqle.getMessage());
                // UNIQUE VIOLATION
                case 23505: return extractUsingTemplate("violates unique constraint "",""", sqle.getMessage());
                // FOREIGN KEY VIOLATION
                case 23503: return extractUsingTemplate("violates foreign key constraint "",""", sqle.getMessage());
                // NOT NULL VIOLATION
                case 23502: return extractUsingTemplate("null value in column "","" violates not-null constraint", sqle.getMessage());
                // TODO: RESTRICT VIOLATION
                case 23001: return null;
                // ALL OTHER
                default: return null;
            }
        } catch (NumberFormatException nfe) {
            return null;
        }
    }
};


Hope this helps.

Regards,
Andrei.


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