Merge branch 'HHH-5811-ByteArrayLob'

This commit is contained in:
Gail Badner 2011-01-04 12:59:24 -08:00
commit 35bf5300c3
9 changed files with 652 additions and 185 deletions

View File

@ -0,0 +1,88 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.annotations.lob;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.MappedSuperclass;
/**
* @author Gail Badner
*/
@MappedSuperclass
public class AbstractBook {
private String shortDescription;
private String fullText;
private Character[] code;
private char[] code2;
private Editor editor;
public String getShortDescription() {
return shortDescription;
}
public void setShortDescription(String shortDescription) {
this.shortDescription = shortDescription;
}
@Lob
@Column(name = "fld_fulltext")
public String getFullText() {
return fullText;
}
public void setFullText(String fullText) {
this.fullText = fullText;
}
@Lob
@Column(name = "fld_code")
public Character[] getCode() {
return code;
}
public void setCode(Character[] code) {
this.code = code;
}
@Lob
public char[] getCode2() {
return code2;
}
public void setCode2(char[] code2) {
this.code2 = code2;
}
@Lob
public Editor getEditor() {
return editor;
}
public void setEditor(Editor editor) {
this.editor = editor;
}
}

View File

@ -0,0 +1,63 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.annotations.lob;
import javax.persistence.Lob;
import javax.persistence.MappedSuperclass;
/**
* @author Gail Badner
*/
@MappedSuperclass
public class AbstractCompiledCode {
private Byte[] header;
private byte[] fullCode;
private byte[] metadata;
public byte[] getMetadata() {
return metadata;
}
public void setMetadata(byte[] metadata) {
this.metadata = metadata;
}
@Lob
public Byte[] getHeader() {
return header;
}
public void setHeader(Byte[] header) {
this.header = header;
}
@Lob
public byte[] getFullCode() {
return fullCode;
}
public void setFullCode(byte[] fullCode) {
this.fullCode = fullCode;
}
}

View File

@ -0,0 +1,168 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.annotations.lob;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.test.annotations.TestCase;
/**
* @author Gail Badner
*/
public abstract class AbstractLobTest<B extends AbstractBook, C extends AbstractCompiledCode> extends TestCase {
public AbstractLobTest(String name) {
super( name );
}
protected abstract Class<B> getBookClass();
protected B createBook() {
try {
return getBookClass().newInstance();
}
catch (Exception ex) {
throw new RuntimeException( "Could not create an instance of type " + getBookClass().getName(), ex );
}
}
protected abstract Integer getId(B book);
protected abstract Class<C> getCompiledCodeClass();
protected C createCompiledCode() {
try {
return getCompiledCodeClass().newInstance();
}
catch (Exception ex) {
throw new RuntimeException( "Could not create an instance of type " + getCompiledCodeClass().getName(), ex );
}
}
protected abstract Integer getId(C compiledCode);
public void testSerializableToBlob() throws Exception {
B book = createBook();
Editor editor = new Editor();
editor.setName( "O'Reilly" );
book.setEditor( editor );
book.setCode2( new char[] { 'r' } );
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
s.persist( book );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
B loadedBook = getBookClass().cast( s.get( getBookClass(), getId( book ) ) );
assertNotNull( loadedBook.getEditor() );
assertEquals( book.getEditor().getName(), loadedBook.getEditor().getName() );
loadedBook.setEditor( null );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
loadedBook = getBookClass().cast( s.get( getBookClass(), getId( book ) ) );
assertNull( loadedBook.getEditor() );
tx.commit();
s.close();
}
public void testClob() throws Exception {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
B b = createBook();
b.setShortDescription( "Hibernate Bible" );
b.setFullText( "Hibernate in Action aims to..." );
b.setCode( new Character[] { 'a', 'b', 'c' } );
b.setCode2( new char[] { 'a', 'b', 'c' } );
s.persist( b );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
B b2 = getBookClass().cast( s.get( getBookClass(), getId( b ) ) );
assertNotNull( b2 );
assertEquals( b2.getFullText(), b.getFullText() );
assertEquals( b2.getCode()[1].charValue(), b.getCode()[1].charValue() );
assertEquals( b2.getCode2()[2], b.getCode2()[2] );
tx.commit();
s.close();
}
public void testBlob() throws Exception {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
C cc = createCompiledCode();
Byte[] header = new Byte[2];
header[0] = new Byte( ( byte ) 3 );
header[1] = new Byte( ( byte ) 0 );
cc.setHeader( header );
int codeSize = 5;
byte[] full = new byte[codeSize];
for ( int i = 0; i < codeSize; i++ ) {
full[i] = ( byte ) ( 1 + i );
}
cc.setFullCode( full );
s.persist( cc );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
C recompiled = getCompiledCodeClass().cast( s.get( getCompiledCodeClass(), getId( cc ) ) );
assertEquals( recompiled.getHeader()[1], cc.getHeader()[1] );
assertEquals( recompiled.getFullCode()[codeSize - 1], cc.getFullCode()[codeSize - 1] );
tx.commit();
s.close();
}
public void testBinary() throws Exception {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
C cc = createCompiledCode();
byte[] metadata = new byte[2];
metadata[0] = ( byte ) 3;
metadata[1] = ( byte ) 0;
cc.setMetadata( metadata );
s.persist( cc );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
C recompiled = getCompiledCodeClass().cast( s.get( getCompiledCodeClass(), getId( cc ) ) );
assertEquals( recompiled.getMetadata()[1], cc.getMetadata()[1] );
tx.commit();
s.close();
}
}

View File

@ -13,13 +13,8 @@ import javax.persistence.Table;
*/
@Entity
@Table(name = "lob_book")
public class Book {
public class Book extends AbstractBook {
private Integer id;
private String shortDescription;
private String fullText;
private Character[] code;
private char[] code2;
private Editor editor;
@Id
@GeneratedValue
@ -30,50 +25,4 @@ public class Book {
public void setId(Integer id) {
this.id = id;
}
public String getShortDescription() {
return shortDescription;
}
public void setShortDescription(String shortDescription) {
this.shortDescription = shortDescription;
}
@Lob
@Column(name = "fld_fulltext")
public String getFullText() {
return fullText;
}
public void setFullText(String fullText) {
this.fullText = fullText;
}
@Lob
@Column(name = "fld_code")
public Character[] getCode() {
return code;
}
public void setCode(Character[] code) {
this.code = code;
}
@Lob
public char[] getCode2() {
return code2;
}
public void setCode2(char[] code2) {
this.code2 = code2;
}
@Lob
public Editor getEditor() {
return editor;
}
public void setEditor(Editor editor) {
this.editor = editor;
}
}

View File

@ -12,19 +12,8 @@ import javax.persistence.Lob;
* @author Emmanuel Bernard
*/
@Entity
public class CompiledCode {
public class CompiledCode extends AbstractCompiledCode {
private Integer id;
private Byte[] header;
private byte[] fullCode;
private byte[] metadata;
public byte[] getMetadata() {
return metadata;
}
public void setMetadata(byte[] metadata) {
this.metadata = metadata;
}
@Id
@GeneratedValue
@ -36,21 +25,4 @@ public class CompiledCode {
this.id = id;
}
@Lob
public Byte[] getHeader() {
return header;
}
public void setHeader(Byte[] header) {
this.header = header;
}
@Lob
public byte[] getFullCode() {
return fullCode;
}
public void setFullCode(byte[] fullCode) {
this.fullCode = fullCode;
}
}

View File

@ -34,115 +34,28 @@ import org.hibernate.test.annotations.TestCase;
* @author Emmanuel Bernard
*/
@RequiresDialectFeature(DialectChecks.SupportsExpectedLobUsagePattern.class)
public class LobTest extends TestCase {
public void testSerializableToBlob() throws Exception {
Book book = new Book();
Editor editor = new Editor();
editor.setName( "O'Reilly" );
book.setEditor( editor );
book.setCode2( new char[] { 'r' } );
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
s.persist( book );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
Book loadedBook = ( Book ) s.get( Book.class, book.getId() );
assertNotNull( loadedBook.getEditor() );
assertEquals( book.getEditor().getName(), loadedBook.getEditor().getName() );
loadedBook.setEditor( null );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
loadedBook = ( Book ) s.get( Book.class, book.getId() );
assertNull( loadedBook.getEditor() );
tx.commit();
s.close();
}
public void testClob() throws Exception {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
Book b = new Book();
b.setShortDescription( "Hibernate Bible" );
b.setFullText( "Hibernate in Action aims to..." );
b.setCode( new Character[] { 'a', 'b', 'c' } );
b.setCode2( new char[] { 'a', 'b', 'c' } );
s.persist( b );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
Book b2 = ( Book ) s.get( Book.class, b.getId() );
assertNotNull( b2 );
assertEquals( b2.getFullText(), b.getFullText() );
assertEquals( b2.getCode()[1].charValue(), b.getCode()[1].charValue() );
assertEquals( b2.getCode2()[2], b.getCode2()[2] );
tx.commit();
s.close();
}
public void testBlob() throws Exception {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
CompiledCode cc = new CompiledCode();
Byte[] header = new Byte[2];
header[0] = new Byte( ( byte ) 3 );
header[1] = new Byte( ( byte ) 0 );
cc.setHeader( header );
int codeSize = 5;
byte[] full = new byte[codeSize];
for ( int i = 0; i < codeSize; i++ ) {
full[i] = ( byte ) ( 1 + i );
}
cc.setFullCode( full );
s.persist( cc );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
CompiledCode recompiled = ( CompiledCode ) s.get( CompiledCode.class, cc.getId() );
assertEquals( recompiled.getHeader()[1], cc.getHeader()[1] );
assertEquals( recompiled.getFullCode()[codeSize - 1], cc.getFullCode()[codeSize - 1] );
tx.commit();
s.close();
}
public void testBinary() throws Exception {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
CompiledCode cc = new CompiledCode();
byte[] metadata = new byte[2];
metadata[0] = ( byte ) 3;
metadata[1] = ( byte ) 0;
cc.setMetadata( metadata );
s.persist( cc );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
CompiledCode recompiled = ( CompiledCode ) s.get( CompiledCode.class, cc.getId() );
assertEquals( recompiled.getMetadata()[1], cc.getMetadata()[1] );
tx.commit();
s.close();
}
public class LobTest extends AbstractLobTest<Book, CompiledCode> {
public LobTest(String x) {
super( x );
}
protected Class<Book> getBookClass() {
return Book.class;
}
protected Integer getId(Book book) {
return book.getId();
}
protected Class<CompiledCode> getCompiledCodeClass() {
return CompiledCode.class;
}
protected Integer getId(CompiledCode compiledCode) {
return compiledCode.getId();
}
protected Class[] getAnnotatedClasses() {
return new Class[] {
Book.class,

View File

@ -0,0 +1,61 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.annotations.lob;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Version;
/**
* @author Gail Badner
*/
@Entity
@Table(name = "ver_lob_book")
public class VersionedBook extends AbstractBook{
private Integer id;
private Integer version;
@Id
@GeneratedValue
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Version
@Column(name = "ver")
public Integer getVersion() {
return version;
}
public void setVersion(Integer i) {
version = i;
}
}

View File

@ -0,0 +1,61 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.annotations.lob;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Version;
/**
* Compiled code representation with a version
*
* @author Gail Badner
*/
@Entity
public class VersionedCompiledCode extends AbstractCompiledCode{
private Integer id;
private Integer version;
@Id
@GeneratedValue
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Version
@Column(name = "ver")
public Integer getVersion() {
return version;
}
public void setVersion(Integer i) {
version = i;
}
}

View File

@ -0,0 +1,192 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.annotations.lob;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.testing.junit.DialectChecks;
import org.hibernate.testing.junit.FailureExpected;
import org.hibernate.testing.junit.RequiresDialectFeature;
import org.hibernate.test.annotations.TestCase;
/**
* @author Gail Badner
*/
@RequiresDialectFeature(DialectChecks.SupportsExpectedLobUsagePattern.class)
public class VersionedLobTest extends AbstractLobTest<VersionedBook, VersionedCompiledCode> {
public VersionedLobTest(String x) {
super( x );
}
protected Class<VersionedBook> getBookClass() {
return VersionedBook.class;
}
protected Integer getId(VersionedBook book) {
return book.getId();
}
protected Class<VersionedCompiledCode> getCompiledCodeClass() {
return VersionedCompiledCode.class;
}
protected Integer getId(VersionedCompiledCode compiledCode) {
return compiledCode.getId();
}
protected Class[] getAnnotatedClasses() {
return new Class[] {
VersionedBook.class,
VersionedCompiledCode.class
};
}
public void testVersionUnchangedPrimitiveCharArray() throws Exception {
VersionedBook book = createBook();
Editor editor = new Editor();
editor.setName( "O'Reilly" );
book.setEditor( editor );
book.setCode2( new char[] { 'r' } );
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
s.persist( book );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
VersionedBook loadedBook = getBookClass().cast( s.get( getBookClass(), getId( book ) ) );
assertEquals( loadedBook.getVersion(), Integer.valueOf( 0 ) );
s.flush();
assertEquals( loadedBook.getVersion(), Integer.valueOf( 0 ) );
s.delete( loadedBook );
tx.commit();
s.close();
}
public void testVersionUnchangedCharArray() throws Exception {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
VersionedBook b = createBook();
b.setShortDescription( "Hibernate Bible" );
b.setCode( new Character[] { 'a', 'b', 'c' } );
s.persist( b );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
VersionedBook b2 = getBookClass().cast( s.get( getBookClass(), getId( b ) ) );
assertNotNull( b2 );
assertEquals( b2.getCode()[1].charValue(), b.getCode()[1].charValue() );
assertEquals( b2.getVersion(), Integer.valueOf( 0 ) );
s.flush();
assertEquals( b2.getVersion(), Integer.valueOf( 0 ) );
s.delete( b2 );
tx.commit();
s.close();
}
public void testVersionUnchangedString() throws Exception {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
VersionedBook b = createBook();
b.setShortDescription( "Hibernate Bible" );
b.setFullText( "Hibernate in Action aims to..." );
s.persist( b );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
VersionedBook b2 = getBookClass().cast( s.get( getBookClass(), getId( b ) ) );
assertNotNull( b2 );
assertEquals( b2.getFullText(), b.getFullText() );
assertEquals( b2.getVersion(), Integer.valueOf( 0 ) );
s.flush();
assertEquals( b2.getVersion(), Integer.valueOf( 0 ) );
s.delete( b2 );
tx.commit();
s.close();
}
@FailureExpected( jiraKey = "HHH-5811")
public void testVersionUnchangedByteArray() throws Exception {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
VersionedCompiledCode cc = createCompiledCode();
Byte[] header = new Byte[2];
header[0] = new Byte( ( byte ) 3 );
header[1] = new Byte( ( byte ) 0 );
cc.setHeader( header );
s.persist( cc );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
VersionedCompiledCode recompiled = getCompiledCodeClass().cast( s.get( getCompiledCodeClass(), getId( cc ) ) );
assertEquals( recompiled.getHeader()[1], cc.getHeader()[1] );
assertEquals( recompiled.getVersion(), Integer.valueOf( 0 ) );
s.flush();
assertEquals( recompiled.getVersion(), Integer.valueOf( 0 ) );
s.delete( recompiled );
tx.commit();
s.close();
}
public void testVersionUnchangedPrimitiveByteArray() throws Exception {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
VersionedCompiledCode cc = createCompiledCode();
int codeSize = 5;
byte[] full = new byte[codeSize];
for ( int i = 0; i < codeSize; i++ ) {
full[i] = ( byte ) ( 1 + i );
}
cc.setFullCode( full );
s.persist( cc );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
VersionedCompiledCode recompiled = getCompiledCodeClass().cast( s.get( getCompiledCodeClass(), getId( cc ) ) );
assertEquals( recompiled.getFullCode()[codeSize - 1], cc.getFullCode()[codeSize - 1] );
assertEquals( recompiled.getVersion(), Integer.valueOf( 0 ) );
s.flush();
assertEquals( recompiled.getVersion(), Integer.valueOf( 0 ) );
s.delete( recompiled );
tx.commit();
s.close();
}
}