tags:

views:

766

answers:

3

Hi every one, 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
A: 

Is the problem on the line before?

@Lob

Should be @Blob maybe?

Skip Head
@Blob is not a hibernate annotation
Am1rr3zA
+1  A: 

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.

ChssPly76
i don't unserstand what you mean!
Am1rr3zA
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.
ChssPly76
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!!!
Am1rr3zA
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.
ChssPly76
Tanx man I consider it
Am1rr3zA
+1  A: 

Hi Amirreza. The reason that NetBeans is generating this warning is that the @Basic and @Lob annotations are mutually exclusive 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.

(edited to correct my earlier statement)

Matt Solnit
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
Am1rr3zA
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?
Matt Solnit