Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

False unique contsraint violation error when using the Java API in groovy

PD Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 1, 2020

I've been making some customization in our environment that involves object creation automatically from a jira post function.

I'm using groovy to create the new object.

But I'm getting a unique constraint violation error on an attribute that I know to have a different value than any that exist.

As a test scenario, I created a brand new object type in my schema (typeId=50) with only the default attributes. I made the "name" attribute unique.

Then I wrote this simplified script to create a new value. I run this from the console:

import com.atlassian.jira.component.ComponentAccessor
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeFacade
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeAttributeFacade

def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(ObjectFacade)
def objectTypeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(ObjectTypeFacade)
def objectTypeAttributeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(ObjectTypeAttributeFacade)

def typeId = 50
def newObjectName = 'test3'

def objectType = objectTypeFacade.loadObjectType(typeId)
def objectTypeAttributes = objectTypeAttributeFacade.findObjectTypeAttributeBeans(objectType.id)
def newObject = objectType.createMutableObjectBean()

def nameOTAB = objectTypeAttributes.find{it.name=='Name'}
def nameOAB = newObject.createObjectAttributeBean( nameOTAB)
def nameOAVB = nameOAB.createObjectAttributeValueBean()

nameOAVB.setValue(nameOTAB, newObjectName)
nameOAB.setObjectAttributeValueBeans([nameOAVB])

newObject.setObjectAttributeBeans([nameOAB])
objectFacade.validateObjectBean(newObject)
log.info "Validation passed"
def object = objectFacade.storeObjectBean(newObject)

This fails on the storeObjectBean() method even when I know for sure that newObjectName 'test3' does not exist.

As proof, if an object with that Name exists, then the script fails on the validateObjectBean() method.

If I remove the Unique flag from the attribute, then the script works fine (of course whether or not the name already exists, potentially creating duplicates).

Am I doing something wrong in how I'm building up my mutableObjectBean? 

Any examples out there of how to create a new object that has a unique attribute?

1 answer

1 accepted

0 votes
Answer accepted
PD Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 2, 2020

Well, nevermind ... I think I found my issue.

If I remove my line to validate the object then the store object works correctly. So, somehow the validateObjectBean must be creating a cached version of the object that the storeObjectBean trips over.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events