GRAILS - GORM : DuplicateKeyException during saving a new object - spring

I use GORM to back occurrences in a database, from an excel file.
new ExcelBuilder(excelFile.inputStream).eachLine([labels: true, sheet: 0]) {
if (cell(0)) {
def nameA = cell(0)
def nameB = cell(1)
def a = Chapitre.findByNom(nameA)
def code = cell(2)
def designation = cell(3)
if (code == null || nameA == null || nameB == null) {
flash.messages << "error"
} else if (!Chapitre.findByNom(nameA)) {
flash.messages << "error"
} else if ( Rubrique.where{nom == nameB && chapitre == a}.list().size() == 0) {
flash.messages << "error"
} else if(Object.where{rubrique == Rubrique.findByNom(nameB) && c == code && d == designation}.count() > 0){
flash.messages << "error"
} else {
def b = Rubrique.findByNom(nameB)
def isNew = false;
Object.withNewSession {session2->
def object = Object.findOrCreateByCode(code)
if(object.designation == null)
isNew = true;
object.rubrique = b
object.d= (designation == null)?"":designation
// try {
rowCount += object.save()? 1 : 0
// } catch(ValidationException) {
// if(isNew)
// rowCount++;
// log.info("ErreuRRRRRRRRrrrRRrrRrRRrrrrRrrrRrrrRrrrr")
// }
}
}
}
currentLine++
}
flash.messages << "${rowCount} ligne create or update"
An update will break any worries, the course of the lines of file continue and database recording is effective.
However when it comes to inserting a new object, I get an Exception:
org.springframework.dao.DuplicateKeyException: a different object with the same identifier value was already associated with the session:[fen.NuisanceType#2202];
nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session
The registration of the object in question is effective , but the error raised off the path of the file.
When i uncommented the try and catch I bypasses the error and so all my copies of each file are created in the database .
I thus found a way around my worries, but I do not find it very clean and i come to you to try to understand my problem.

Without further information hard to give any clear answers. Is this code back in a service?(very much doubt it since it has flash.message pointing to controller doing all of it.) Try making it into a service and transactional then maybe you could look at removing withNewTransaction call.
You can read more about error created here:
Grails - DuplicateKeyException
Review comment:
"Well, that problem occurs when you are initializing a class new ClassD with an id or unique property manually, when there is another already in the session. So, you should try to get that entity first (that's what findOrCreateWhere does, but if you use an id you need to use get) and then use the instance found or create a new one for the update"
Hibernate Error: a different object with the same identifier value was already associated with the session
You code tidied up and running from service: (issues may go away) since I have also cleaned up the duplicated finds you were doing:
class TestService {
static transactional=true
def saveRecord() {
def results=[]
new ExcelBuilder(excelFile.inputStream).eachLine([labels: true, sheet: 0]) {
if (cell(0)) {
def nameA = cell(0)
def nameB = cell(1)
def code = cell(2)
def designation = cell(3)
def a = Chapitre.findByNom(nameA)
def b = Rubrique.where{nom == nameB && chapitre == a}
def c = Object.where{rubrique == b && c == code && d == designation}
if (!code||!nameA||!nameB||!a||!b||!c) {
results << "Error saving ${nameA} ${nameB} ${code}"
} else {
//boolean isNew = false
def object = Object.findOrSaveWhere(code:code)
if(object) {
if (!object.designation) {
rowCount++
results << "Record ${object} has no designation ? new Record?"
}
object.rubrique = b
object.d = designation ?: ''
object.save()
results << "Record ${object.id} is saved"
} else {
/*
* Could not save or find code:code now create a new object:
* object = new Object(code:code, rubrique:rubrique: d: designation ?: '').save()
*
*/
}
}
}
currentLine++
}
results << "${rowCount} ligne create or update"
return results
}
}

Related

"invalid sequence of tokens near ['for']" error in painless script query

I am getting error while executing this script query using Nest library in .net:
new ScriptQuery
{
Lang = "painless",
Source = "(!doc[params.headquartersCoordinatesField].empty && doc[params.headquartersCoordinatesField].arcDistance(params.latitude, params.longitude) * 0.000621371 <= params.maxDistance) || (!doc[params.offices].empty && (for (def office : doc[params.offices].values){if(office.coordinates).arcDistance(params.latitude, params.longitude) * 0.000621371 < =params.maxDistance{return true;}}))",
Params = new Dictionary<string, object>
{
{"headquartersCoordinatesField", Field<Provider>(f => f.Headquarters.Coordinates)},
{"offices", Field<Provider>(f => f.Offices)},
{"latitude", _latitude},
{"longitude", _longitude},
{"maxDistance", 50}
}
}
This is the error I get :
ServerError: Type: search_phase_execution_exception Reason: "all shards failed" CausedBy: "Type: script_exception Reason: "compile error" CausedBy: "Type: illegal_argument_exception Reason: "invalid sequence of tokens near ['for']." CausedBy: "Type: no_viable_alt_exception Reason: "no_viable_alt_exception: null""""
I also tried boolean variable inside loop and try to return that at the end but I get the same error.
I tried simple for loop with counter (i) to check the syntax but same error. So it seems like anything I use inside loop is returning error.
Can someone help to find the correct syntax ? Thanks in advance.
You cannot have a for loop inside a condition, it doesn't make sense.
Here is how your script should look like:
def hqCoordExist = !doc[params.headquartersCoordinatesField].empty;
def distToHq = doc[params.headquartersCoordinatesField].arcDistance(params.latitude, params.longitude);
if (hqCoordExist && distToHq * 0.000621371 <= params.maxDistance) {
return true;
}
def officesExist = !doc[params.offices].empty;
if (officesExist) {
for (def office : doc[params.offices].values) {
def distToOffice = office.coordinates.arcDistance(params.latitude, params.longitude);
if (distToOffice * 0.000621371 <= params.maxDistance) {
return true;
}
}
}
return false;
This worked for me. I had to check key and size of offices param. :
if(!doc[params.headquartersCoordinatesField].empty && doc[params.headquartersCoordinatesField].arcDistance(params.latitude, params.longitude) * 0.000621371 <= params.maxDistance)
{
return true;
}
def officesCollection = new ArrayList();
if(doc.containsKey(params.offices) && doc[params.offices].size() > 0)
{
officesCollection = doc[params.offices].value;
for (def office : officesCollection)
{
def distToOffice =
office.coordinates.arcDistance(params.latitude, params.longitude);
if (distToOffice * 0.000621371 <= params.maxDistance)
{
return true;
}
}
}
return false;

Kotlin Cannibals and Missionaries Algorithm - How to convert to full FP

Any suggestions on how to improve the following code to make it more Functional Programming oriented. Specifically how to remove the MutableList which signifies historical states. There are two data classes: Bank, which represents a riverbank (number of missionaries and number of cannibals currently on the bank) and BankState which represents a historical state of the two banks (the source bank, target bank and boatAtSource - a boolean which indicates whether the boat is currently at the source or target bank). overloaded operator function plus adds missionaries and cannibals to a riverbank and function minus removes them from a riverbank. The boat function is the one which carries the most heft. You can call the following algorithm from fun main (app.kt) as such:
app.kt
fun main(args:Array<String>) {
val source:Bank = Bank(3,3)
val target:Bank = Bank()
source boat target
}
Bank.kt
data class Bank(val missionaries:Int=0,val cannibals:Int=0)
data class BankState(val sourceTarget:Pair<Bank,Bank>,val boatAtSource:Boolean)
operator fun Bank.plus(b:Pair<Int,Int>):Bank = Bank(this.missionaries+b.first,this.cannibals+b.second)
operator fun Bank.minus(b:Pair<Int,Int>):Bank = Bank(this.missionaries-b.first,this.cannibals-b.second)
infix fun Bank.boat(target:Bank):List<BankState> {
val begin = Pair(this,target)
val history = mutableListOf<BankState>(BankState(begin,true))
boat(begin,true,this.missionaries,this.cannibals,history)
return history
}
fun boat(sourceTarget:Pair<Bank,Bank>,
boatAtSource:Boolean,
totalMissionaries:Int,
totalCannibals:Int,
history:MutableList<BankState>):Boolean {
if(sourceTarget.first.cannibals+sourceTarget.second.cannibals==totalCannibals &&
sourceTarget.first.missionaries + sourceTarget.second.missionaries==totalMissionaries &&
sourceTarget.first.cannibals>=0 &&
sourceTarget.first.missionaries>=0 &&
sourceTarget.second.cannibals>=0 &&
sourceTarget.second.missionaries>=0 &&
(sourceTarget.first.missionaries==0 || sourceTarget.first.missionaries>=sourceTarget.first.cannibals) &&
(sourceTarget.second.missionaries==0 || sourceTarget.second.missionaries >= sourceTarget.second.cannibals)) {
if(sourceTarget.second.missionaries==totalMissionaries &&
sourceTarget.second.cannibals==totalCannibals) {
history.forEach(::println)
return true
} else {
val deltas = listOf(Pair(0,1),Pair(1,1),Pair(1,0),Pair(2,0),Pair(0,2))
val comparator = object : Comparator<Pair<Pair<Boolean,Int>,Pair<Bank,Bank>>> {
override fun compare(arg1:Pair<Pair<Boolean,Int>,Pair<Bank,Bank>>,arg2:Pair<Pair<Boolean,Int>,Pair<Bank,Bank>>):Int {
if(arg1.first.first && arg2.first.first) {
return if(arg1.first.second<arg2.first.second) -1 else if(arg1.first.second>arg2.first.second) 1 else 0
} else if(arg1.first.first){
return 1
} else if(arg2.first.first) {
return -1
}
return 0
}
}
val result = deltas.map{
checkNext(it.first,it.second,totalMissionaries,totalCannibals,history,sourceTarget,boatAtSource)
}.maxWith(comparator)
if(result?.first?.first!=null && result.first.first) {
history.add(BankState(result.second,!boatAtSource))
return true;
}
}
}
return false
}
fun checkNext(missionariesDelta:Int,
cannibalsDelta:Int,
totalMissionaries:Int,
totalCannibals:Int,
history:MutableList<BankState>,
sourceTarget:Pair<Bank,Bank>,
boatAtSource:Boolean):Pair<Pair<Boolean,Int>,Pair<Bank,Bank>> {
val nextSrcTgt = if(boatAtSource) Pair(sourceTarget.first-Pair(missionariesDelta,cannibalsDelta),sourceTarget.second+Pair(missionariesDelta,cannibalsDelta))
else Pair(sourceTarget.first+Pair(missionariesDelta,cannibalsDelta),sourceTarget.second-Pair(missionariesDelta,cannibalsDelta))
val bankState:BankState = BankState(nextSrcTgt,!boatAtSource)
if(!history.contains(bankState)) {
history.add(bankState)
val combo2:Boolean = boat(nextSrcTgt,!boatAtSource,totalMissionaries,totalCannibals,history)
val combo2Depth = history.size
history.remove(bankState)
return Pair(Pair(combo2,combo2Depth),nextSrcTgt)
} else {
return Pair(Pair(false,0),nextSrcTgt)
}
}

grails ajax onload and oncomplete

can anyone give me sample coding that using grails ajax onload and oncomplete functions on following reader action? i want show a spinner icon while this action is onload and redirect to the list page while action is oncomplete..
def reader(){
def list = []
def dir = new File("C:/Users/User/Desktop/Summarize_20141212/HR_FILE")
dir.eachFileRecurse(FileType.FILES) { file ->
list << file
}
println list
list.each {
File file = new File(it.path)
def sql = groovy.sql.Sql.newInstance("jdbc:postgresql://localhost:5432/new",
'postgres', 'sa', "org.postgresql.Driver")
def lincoln = 0
file.eachLine() { line ->
if (line.trim().size() == 0) {
return null
} else {
def field = []
def tokens = line.split(',(?=([^\"]*\"[^\"]*\")*[^\"]*$)')
file.name.lastIndexOf('.').with { it != -1 ? file.name[0..<it] : file.name }
lincoln++
field.push(file.name)
for (String t : tokens) {
field.push(t)
}
while (field.size() < 10) {
field.push("")
}
if (lincoln > 1) {
sql.execute('insert into read_hr(version,col01,col02,col03,col04,col05,col06,col07,col08,col09,col10)' +
'VALUES (0,?,?,?,?,?,?,?,?,?,?)', field)
System.out.println(field);
}
}
}
}
redirect (action:"list")
}
These link will useful to you .
http://www.javacodegeeks.com/2013/05/grails-ajax-examples.html
http://javadeveloper.asia/grails-ajax-tutorial-remotelink-tag

Comparing objects before and after update retrieved by LINQ query

I am using LINQtoSQL and I need to compare object before and after update.
In the example below Student 'before' and Student 'curStudent' are the same, because they was retrieved by the same query.
using(DataContext db = new DataContext())
{
Student before = db.Student.Where(q=>q.id == 1).SingleOrDefault();
Student curStudent = db.Student.Where(q=>q.id == 1).SingleOrDefault();
curStudent.Name = "NewName";
db.SubmitChanges();
}
if(before.Name != curStudent.Name) // this condition will never be true
{
//do something
}
The context will only keep one object per key value, so one way would be to use two separate contexts:
Student before;
using(DataContext db1 = new DataContext())
{
before = db.Student.Where(q=>q.id == 1).SingleOrDefault();
}
Student curStudent;
using(DataContext db2 = new DataContext())
{
curStudent = db.Student.Where(q=>q.id == 1).SingleOrDefault();
curStudent.Name = "NewName";
db.SubmitChanges();
}
if(before.Name != curStudent.Name)
{
//do something
}
Or if you're just interested in the name change, keep the previous value:
string before;
using(DataContext db = new DataContext())
{
Student curStudent = db.Student.Where(q=>q.id == 1).SingleOrDefault();
before = curStudent.Name; // cache the name
curStudent.Name = "NewName";
db.SubmitChanges();
}
if(before != curStudent.Name)
{
//do something
}

Saving of grails domain object with an attribute type object fails

I'm trying to save a domain object with grails 2.3.3 But it is not saved. How can I save it and why is it not saving?
The domain code:
package berg
import nl.jappieklooster.Log
class FieldValue {
Object value
public String toString(){
Log.debug "Field value: {0}", value
return value.toString()
}
static constraints = {
}
}
The code that saves:
// an extract from the bootsrap file
def init = { servletContext ->
def blueFV = new FieldValue(value: Color.blue)
def smallFV = new FieldValue(value: "small")
def fieldVals = [blueFV, smallFV]
saveData(fieldVals,berg.FieldValue)
}
public void saveData(List list, Class type){
def wholeList = type.list() ?: []
println("Started with adding the "+type.getName()+" classes.")
int saved = 0;
int failed = 0;
if(!wholeList){
list.each{ i ->
if(i.validate()){
i.save(flush:true, failOnError: true)
saved++
}
else{
println("! - - - Warning: '"+i.toString()+"' could not be created! - - - !")
failed++
}
}
if(failed > 0)//if one fails, let the message appear more clearly
println(".v.v.")
println("When saving the "+type.getName()+" classes: "+saved+" were saved, "+failed+" failed to be saved.")
if(failed > 0)
println(".^.^.")
}
}
The entire value column does not show up in the database

Resources