Hibernate: constraintName is null in MySQL -


I have Hibernate 3.3.x. With a code using JPA. This Java code can be used with Oracle 10G or schematic stored on MySQL 5.1.x. Tables are defined to define unique records. When a barrier is violated, I want to get the name of the obligation with the exception.

With Oracle, the name of the hindrance is properly recovered. With MySQL, the compulsion name is NULL .

Any idea how to get the name of the compulsion with MySQL?

thanks

I have the following solution I came with:

  1. Existing Hibernate MySQL5Dialect extension:

      Public class extends MySQL5Dialect org.hibernate.dialect.MySQL5Dialect {/ ** * {@link SQLException} to remove compulsion name from violation. * / Personal static final pattern PATTERN = Pattern.compile (". * Barrier \\ W + (\\ w +). *", Pattern.CASE_INSENSITIVE); Private Infringement Insert Extractor Obstacle Designer; Public MySQL5Dialect () {constraintNameExtracter = New ConstraintNameExtractor (); } @ Override Public Vaulted ConstantName Extractor has been violated by the Combined Extractor () {Return Binding Nuclear Extractor; } Private class constraintNameExtractor applies ViolatedConstraintNameExtracter {@Override Public String ExtractConstraintName {SQLException sqle} {last string msg = sqle.getMessage (); Last Matcher matcher = PATTERN.matcher (msg); String barpoint = null; If (matcher.matches ()) {constraint name = matcher.group (1); } Return binding; Specify the newly created bid in the hibernate config file (hibernate.cfg.xml):  
      & lt; Property Name = "Bid" & gt; Your.package.MySQL5Dialect & lt; / Property & gt;  
  2. Now getConstraintName () will return the actual infringed interrupt name:

      try {...} hold (constraint violation) ) {LOG.error (String.format ("barrier =% s, code =% d", e.getConstraintName (), e.getErrorCode ())); }  

Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -