1

I have this code

@Entity
@Table(name = "picture")
public class Picture implements Serializable {
    @Id
    @Column(name = "id")
    @GeneratedValue
    private int id;
    @Column(name = "format", length = 8)
    private String format;

    @Basic(fetch = FetchType.LAZY)
    @Column(name = "context", nullable = true, columnDefinition="mediumblob")
    @Lob
    private java.sql.Blob myBlobAttribute; // protected accessor and modifier

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "branch_fk", referencedColumnName = "id", nullable = false)
    private Branch branch;
//Some setter and getter

I use netbeans 6.7 and in this Ide it show me error on line (private java.sql.Blob myBlobAttribute;) but code run and it's make picture table on my database! is it a real error or just e notification and how must I solve it? error message was:

basic attributes can only be of the following types: java primitive types,wrapper of primitive types, String, java.math.bigInteger, java.math.BigDecimal, java,util.Date, java.util.Calendar, java.sql.Data, java.sql.TimeStamp, byte[], Byte[], char[], Character[], enums, or any Serializable type
1
  • 'Basic' attribute type should not be 'Blob' (see. below) Commented Nov 21, 2012 at 17:03

3 Answers 3

2

The reason that NetBeans is generating this warning is that when using java.sql.Blob, you should only have the @Lob annotation and not @Basic.

However, at runtime, it sounds like your JPA implementation is "helping you out" by ignoring the @Basic annotation and recognizing that the column is in fact a LOB. This is why your code works. It is possible that a different JPA implementation would fail or somehow behave differently.

Sign up to request clarification or add additional context in comments.

2 Comments

ok I remove @Basic annotation from the code but I still have error!!! What should I do that netbeans don't show the error? Tanx For your answer
Hi Amirreza. Are you saying that even when the @Basic annotation is not there, you still get the exact same "basic attributes can only be of the following types" warning from NetBeans?
1

Your property type is java.sql.Blob which is an interface.
First of all, why? Shouldn't it be a byte array (presumably that's where you store your image)?
Secondly, that's why NetBeans complains - and so will Hibernate once you try to read stuff from this table - they have no way of knowing what actual type to create to put data in your field.

5 Comments

i don't unserstand what you mean!
Your property is declared as private java.sql.Blob myBlobAttribute;. java.sql.Blob is an interface. How would Hibernate know what to populate it with? It's not one of types for which @Basic annotation is supported - hence the error you're getting. I'm suggesting you declare your property as byte[] instead.
Tanx, it's a good idea but My question is why this code run and make picture table and also make a context field (mediumblob size) in a correct way and size? why ignore the error? this is strange for me!!!
By "code ran" you mean that your table schema was created (either via hbm2ddl or using autoupdate). That part worked, because you've explicitly specified column definition. If you want to see your code fail, write a unit test that stores something into that table and then tries to read it back.
@ChssPly hi i'm using com.mysql.jdbc.Blob as image to save in DB. also we are using cluster Environment. so ERROR: byte[] can not be serialized after googling i found serializedBlob but giving me same ERROR basic attributes can only be of the ..
0

Is the problem on the line before?

@Lob

Should be @Blob maybe?

1 Comment

@Blob is not a hibernate annotation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.