Skip to content

Commit

Permalink
OGM-952 Avoiding update of converted value despite it hasn't changed;
Browse files Browse the repository at this point in the history
This was caused by the fact that the converter operation isn't symmetric and the value didn't implement equals(), so a value change was wrongly detected. This causes issues with CouchDB (see OGM-954).
  • Loading branch information
gunnarmorling committed Jan 8, 2016
1 parent b717293 commit 8226e85
Showing 1 changed file with 37 additions and 7 deletions.
Expand Up @@ -6,18 +6,18 @@
*/
package org.hibernate.ogm.backendtck.type.custom;

import static org.fest.assertions.Assertions.assertThat;

import java.net.URL;
import javax.persistence.AttributeConverter;

import org.junit.Test;
import javax.persistence.AttributeConverter;

import org.hibernate.Session;
import org.hibernate.ogm.OgmSessionFactory;
import org.hibernate.ogm.cfg.OgmConfiguration;
import org.hibernate.ogm.utils.OgmTestCase;
import org.hibernate.type.descriptor.java.UrlTypeDescriptor;

import static org.fest.assertions.Assertions.assertThat;
import org.junit.Test;

/**
* Test the JPA @Convert logic in OGM
Expand All @@ -39,8 +39,8 @@ public void testJpaConvert() throws Exception {
Session session = openSession();
session.getTransaction().begin();
Printer printer = new Printer();
printer.name = new MyString( "SomeFoo" );
assertThat( printer.name.toString() ).isEqualTo( "SomeFoo" );
printer.name = new MyString( "somefoo" );
assertThat( printer.name.toString() ).isEqualTo( "somefoo" );
session.persist( printer );
session.getTransaction().commit();

Expand Down Expand Up @@ -130,11 +130,41 @@ public MyString(String string) {
this.string = string;
}


@Override
public String toString() {
return string;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ( ( string == null ) ? 0 : string.hashCode() );
return result;
}

@Override
public boolean equals(Object obj) {
if ( this == obj ) {
return true;
}
if ( obj == null ) {
return false;
}
if ( getClass() != obj.getClass() ) {
return false;
}
MyString other = (MyString) obj;
if ( string == null ) {
if ( other.string != null ) {
return false;
}
}
else if ( !string.equals( other.string ) ) {
return false;
}
return true;
}
}

public static class URLToMyStringConverter implements AttributeConverter<URL, MyString> {
Expand Down

0 comments on commit 8226e85

Please sign in to comment.