Using BLOB fields with JPA

C

carmelo

Hi everybody,
I'd like to have your suggestions on how to work with a BLOB field on
a MySql db, on which I need to store an image, using JPA.

The table is:
MyTable(id, code, description, BLOB image)

I developed a Swing app using Netbeans 6.5.1, making binding between
MyTable fields and some JTextFields. The JPA implementation used is
TopLink.

How can I do to store the imagi into the BLOB field?


Thank you for your help
 
A

Arne Vajhøj

carmelo said:
I'd like to have your suggestions on how to work with a BLOB field on
a MySql db, on which I need to store an image, using JPA.

The table is:
MyTable(id, code, description, BLOB image)

I developed a Swing app using Netbeans 6.5.1, making binding between
MyTable fields and some JTextFields. The JPA implementation used is
TopLink.

How can I do to store the imagi into the BLOB field?

http://www.oracle.com/technology/products/ias/toplink/jpa/resources/toplink-jpa-annotations.html#Lob

Arne
 
C

carmelo


Thank you for your answer Arne.
I was able to define the blob field using JPA. My problem is that I'm
not able to store the picture, using data binding.

I'm trying to write a Converter class, but I get runtime casting
errors. The Converter class is this:

public class ImageConverter extends Converter
{
//byte to image
public ImageIcon convertForward(Object arg)
{
Image image = new ImageIcon( ( byte[] ) arg ).getImage()
.getScaledInstance( 50, 50,
Image.SCALE_SMOOTH );

return new ImageIcon( image );
}

//image to byte
public byte[] convertReverse(Object arg)
{
ImageIcon image = (( ImageIcon ) arg );
String s = image.toString();
byte[] b = new byte[s.length()];
for(int i=0; i<s.length(); i++)
b = (byte)s.charAt(i);

return b;
}
}


The stack trace is this:

Caused by: org.jdesktop.beansbinding.PropertyResolutionException:
Error evaluating EL expression ValueExpression[$
{selectedElement.image}] on javax.swing.JTable[masterTable,
0,0,406x48,alignmentX=0.0,alignmentY=0.0,border=,flags=251658568,maximumSize=,minimumSize=,preferredSize=,autoCreateColumnsFromModel=true,autoResizeMode=AUTO_RESIZE_SUBSEQUENT_COLUMNS,cellSelectionEnabled=false,editingColumn=-1,editingRow=-1,gridColor=javax.swing.plaf.ColorUIResource
[r=128,g=128,b=128],preferredViewportSize=java.awt.Dimension
[width=450,height=400],rowHeight=16,rowMargin=1,rowSelectionAllowed=true,selectionBackground=javax.swing.plaf.ColorUIResource
[r=49,g=106,b=197],selectionForeground=javax.swing.plaf.ColorUIResource
[r=255,g=255,b=255],showHorizontalLines=true,showVerticalLines=true]
at org.jdesktop.beansbinding.ELProperty.setValue
(ELProperty.java:507)
at org.jdesktop.beansbinding.Binding.saveUnmanaged
(Binding.java:1275)
at org.jdesktop.beansbinding.Binding.save(Binding.java:1254)
at org.jdesktop.beansbinding.AutoBinding.trySaveThenRefresh
(AutoBinding.java:176)
at org.jdesktop.beansbinding.AutoBinding.targetChangedImpl
(AutoBinding.java:249)
at org.jdesktop.beansbinding.Binding.targetChanged
(Binding.java:1433)
at org.jdesktop.beansbinding.Binding.access$1300(Binding.java:
38)
at org.jdesktop.beansbinding.Binding$PSL.propertyStateChanged
(Binding.java:1620)
at
org.jdesktop.beansbinding.PropertyHelper.firePropertyStateChange
(PropertyHelper.java:212)
at org.jdesktop.beansbinding.BeanProperty.notifyListeners
(BeanProperty.java:712)
at org.jdesktop.beansbinding.BeanProperty.access$1000
(BeanProperty.java:143)
at org.jdesktop.beansbinding.BeanProperty
$SourceEntry.cachedValueChanged(BeanProperty.java:369)
at org.jdesktop.beansbinding.BeanProperty
$SourceEntry.propertyValueChanged(BeanProperty.java:409)
at org.jdesktop.beansbinding.BeanProperty
$SourceEntry.propertyChange(BeanProperty.java:414)
at java.beans.PropertyChangeSupport.firePropertyChange
(PropertyChangeSupport.java:339)
at java.beans.PropertyChangeSupport.firePropertyChange
(PropertyChangeSupport.java:276)
at java.awt.Component.firePropertyChange(Component.java:7868)
at javax.swing.JLabel.setIcon(JLabel.java:371)
at
desktopapplication_blob.DesktopApplication_BLOBView.setPicture
(DesktopApplication_BLOBView.java:574)
at
desktopapplication_blob.DesktopApplication_BLOBView.showOpenFileDialog
(DesktopApplication_BLOBView.java:558)
... 32 more
Caused by: org.jdesktop.el.ELException: java.lang.ClassCastException
at org.jdesktop.el.BeanELResolver.setValue(BeanELResolver.java:
335)
at org.jdesktop.beansbinding.TempELContext
$BeanDelegateELResolver.setValue(TempELContext.java:87)
at org.jdesktop.el.CompositeELResolver.setValue
(CompositeELResolver.java:265)
at org.jdesktop.el.impl.parser.AstValue.setValue(AstValue.java:
133)
at org.jdesktop.el.impl.ValueExpressionImpl.setValue
(ValueExpressionImpl.java:259)
at org.jdesktop.beansbinding.ELProperty.setValue
(ELProperty.java:505)
... 51 more
Caused by: java.lang.ClassCastException
at java.lang.Class.cast(Class.java:2990)
at org.jdesktop.beansbinding.Binding.convertForward
(Binding.java:1312)
at org.jdesktop.beansbinding.Binding.getSourceValueForTarget
(Binding.java:844)
at org.jdesktop.beansbinding.Binding.refreshUnmanaged
(Binding.java:1222)
at org.jdesktop.beansbinding.Binding.refresh(Binding.java:
1207)
at org.jdesktop.beansbinding.AutoBinding.tryRefreshThenSave
(AutoBinding.java:162)
at org.jdesktop.beansbinding.AutoBinding.sourceChangedImpl
(AutoBinding.java:227)
at org.jdesktop.beansbinding.Binding.sourceChanged
(Binding.java:1411)
at org.jdesktop.beansbinding.Binding.access$1200(Binding.java:
38)
at org.jdesktop.beansbinding.Binding$PSL.propertyStateChanged
(Binding.java:1618)
at
org.jdesktop.beansbinding.PropertyHelper.firePropertyStateChange
(PropertyHelper.java:212)
at org.jdesktop.beansbinding.ELProperty.notifyListeners
(ELProperty.java:688)
at org.jdesktop.beansbinding.ELProperty.access$800
(ELProperty.java:155)
at org.jdesktop.beansbinding.ELProperty
$SourceEntry.processSourceChanged(ELProperty.java:312)
at org.jdesktop.beansbinding.ELProperty
$SourceEntry.sourceChanged(ELProperty.java:326)
at org.jdesktop.beansbinding.ELProperty
$SourceEntry.propertyChange(ELProperty.java:333)
at java.beans.PropertyChangeSupport.firePropertyChange
(PropertyChangeSupport.java:339)
at java.beans.PropertyChangeSupport.firePropertyChange
(PropertyChangeSupport.java:276)
at desktopapplication_blob.Prova.setImage(Prova.java:57)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke
(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.jdesktop.el.BeanELResolver.setValue(BeanELResolver.java:
330)
... 56 more
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top