Frage

I want to add a character to my primary as B1,B2,B3 and so on.

I am using sequence to generate the number part of the primary key and in my Entiy class setter method, I am adding B to the number generated. But primary key comumn in the database is always created with only number [sequence value only], B is not appended at all, also when I debugged I found that setter method is not all called when creating this value.

I am using Eclipselink2.4, JPA 2 in weblogic 12c.

Would appeciate your suggestions and solutions.

 @Id
    @Basic(optional = false)
    @Column(name = "BUNDLE_MSG_ID")
    @SequenceGenerator(name="DB_SEQ", sequenceName="DB_SEQ", allocationSize=1)
    @GeneratedValue(strategy= GenerationType.SEQUENCE, generator="DB_SEQ")
    private String bundleMsgId;

    public String getBundleMsgId() {        
      return this.bundleMsgId;
    }

    public void setBundleMsgId(String bundleMsgId) {        
      if( bundleMsgId != null && !bundleMsgId.startsWith("B")){         
         this.bundleMsgId = "B"+bundleMsgId;
      }     
      this.bundleMsgId = bundleMsgId;
    }
War es hilfreich?

Lösung

Using a set method will not work, as the database assigns the sequence value, not the JPA provider. The provider is getting the value from the database, not the other way around.

The best way to accomplish this might be to setup a trigger in the database to assign the sequence, and then JPA Sequence can read in the value using IDENTITY as the GenerationType instead. You may need to indicate that IDENTITY is supported on the platform as described in this post http://dev.eclipse.org/mhonarc/lists/eclipselink-users/msg01320.html

An alternative would be to create your own custom sequencing class, as described here http://wiki.eclipse.org/EclipseLink/Examples/JPA/CustomSequencing . You could use the org.eclipse.persistence.sequencing.NativeSequence class as a model, and just prepend the character as needed.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top