Grails

Constraints for Hibernate mapped classes lead to HibernateException: contains is not valid without active transaction

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Critical Critical
  • Resolution: Not A Bug
  • Affects Version/s: 1.1-beta3
  • Fix Version/s: 1.1-RC1
  • Component/s: Persistence
  • Labels:
    None
  • Environment:
    java version "1.6.0_03"
    Oracle Database 10g Enterprise Edition Release 10.2.0.3.0
    hibernate-annotations-3.3.1.GA
    hibernate-validator-3.1.0.GA
    Windows XP SP3

Description

I have hibernate mapped classes (through annotations) that i have constrainted via this method http://grails.org/Validation (at the bottom Defining constraints for Hibernate mapped classes).

Example:

de.enerko.auftragsabwicklung.eo.common.Marktteilnehmer
@Entity
@Table(name="MARKTTEILNEHMER")
public class Marktteilnehmer {
	@Id @GeneratedValue(generator="id")
	@GenericGenerator(
			name="id", strategy = "sequence",
			parameters = {@org.hibernate.annotations.Parameter(name="sequence", value = "ID")}
	)
	@Column(name="MARKTTEILNEHMER_ID")
	private long id;

       // Getters / Setters follow
}

and

de.enerko.auftragsabwicklung.eo.leistungserfassung.DigitalErfassteLeistung
@Entity
@Table(name="DIGITAL_ERFASSTE_LEISTUNGEN")
@Filters({
	@Filter(
			name="filterByOwner", 			
			condition="(" +
					":isInternalUser = 1 " + 
					" OR EXISTS ( "
          " -- some condition "					
					" ))"		
	)
})
public class DigitalErfassteLeistung {
	@Id @GeneratedValue(generator="dleb_seq")
	@GenericGenerator(
			name="dleb_seq", strategy = "sequence",
			parameters = {@org.hibernate.annotations.Parameter(name="sequence", value = "DLEB_SEQ")}
	)
	private Long id;

	@Column(name="LV_LB_POSITION", nullable=false)
	@NotNull
	private String leistungsbeschreibungPosition;
		
	@Length(max=128)
	@Column(name="BEMERKUNG", nullable=true)
	private String bemerkung;	
	
	@ManyToOne
	@Fetch(value=FetchMode.JOIN)
	@JoinColumn(name="BEARBEITER_ID")
	private Marktteilnehmer bearbeiter;
	
	// getters / setters
}

My constraints are defined as following in $GRAILS_APP/src/java/<package_path>/DigitalErfassteLeistungConstraints.groovy:

package de.enerko.auftragsabwicklung.eo.leistungserfassung;

constraints = {
    leistungsbeschreibungPosition(
      nullable:false,
      blank:false
    )
    kurzbezeichnung(
      nullable:false,
      blank:false
    )
    menge(
      nullable:false,
      blank:false
    )
    bearbeiter(
      nullable:false,
      blank:false
    )
}

The following code leads to the hibernate exception:

def del = DigitalErfassteLeistung.get(686);        
del.bearbeiter = Marktteilnehmer.get(1)
del.save()
def hasErrors = del.hasErrors() || !del.save();
flash.message = "Has errors? ${hasErrors}"

Result is:

org.codehaus.groovy.runtime.InvokerInvocationException: org.hibernate.HibernateException: contains is not valid without active transaction
        at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:92)
        at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:234)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1061)
        at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:910)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:892)
        at groovy.lang.Closure.call(Closure.java:279)
        at groovy.lang.Closure.call(Closure.java:274)

Code works in 1.0.4 but not in 1.1-beta3.

Thanks for looking at the issue!

  1. stacktrace.log
    10/Feb/09 5:31 AM
    10 kB
    Michael Simons

Activity

Hide
Graeme Rocher added a comment -

Would it be possible to post your stacktrace.log file and even better a reproducible sample application?

Show
Graeme Rocher added a comment - Would it be possible to post your stacktrace.log file and even better a reproducible sample application?
Hide
Michael Simons added a comment -

As requested, a runnable app (runs on oracle, table definitions included, may require adaption to mysql or other).

Furthermore, currently reproduced stacktrace.j

Goes away when constraints are removed from java/src directory, but they are needed as validator annotations are not evaluated.

Show
Michael Simons added a comment - As requested, a runnable app (runs on oracle, table definitions included, may require adaption to mysql or other). Furthermore, currently reproduced stacktrace.j Goes away when constraints are removed from java/src directory, but they are needed as validator annotations are not evaluated.
Hide
Michael Simons added a comment -

Maybe interessting:
error goes away when the constraint on the name column is removed.

Show
Michael Simons added a comment - Maybe interessting: error goes away when the constraint on the name column is removed.
Hide
Graeme Rocher added a comment -

The problem is because you have:

<property name="hibernate.current_session_context_class">thread</property>

in your hibernate.cfg.xml, which prevents Grails from correctly looking up the session

Show
Graeme Rocher added a comment - The problem is because you have:
<property name="hibernate.current_session_context_class">thread</property>
in your hibernate.cfg.xml, which prevents Grails from correctly looking up the session
Hide
Michael Simons added a comment -

Thanks Graeme!

I couldn't have solved this, thanks a lot!

The classes are used in a non-JTA environment as well, therefore this fragment.

Show
Michael Simons added a comment - Thanks Graeme! I couldn't have solved this, thanks a lot! The classes are used in a non-JTA environment as well, therefore this fragment.

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: