29

I have an another question concerning JPA. Is it necessary to annotate each member variable with @Column, if i want to store it in my database? Or is it possible to leave it out by some member variables (in these example the field "timestamp") and this field will be stored in database in each possible scenario:

@Entity
@Table(name = "usercontent")
public class UserContentEntity implements Persistable<Long> {

   /** serialVersionUID */
   private static final long serialVersionUID = 1L;

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   private Long id;

   @Column(name = "description")
   private String contentdescription;

   @Column(name = "name")
   private String contentname;

   @Column(name = "bytes")
   @Lob
   private byte[] contentbytes;

   @Column
   private String creator;

   private Date timestamp;


   // get- and set methods
}

And when it is absolutley necessary to use the annotation @Column?

Thanks a lot Maik

1 Answers1

38

No. it's not "necessary", unless you want to override default column naming, or default datastore column type etc. And just putting @Column does nothing as such anyway

Dennis
  • 3,962
  • 7
  • 26
  • 44
Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
  • So member with annotation column and without annotation @Column will be stored in my small example above? Is this right? –  Mar 29 '14 at 19:39
  • 8
    The vast majority of standard java types are default "persistent" (use @Transient to explicitly not persist them). – Neil Stockton Mar 30 '14 at 06:00
  • But what is the situation, if i have for example this memeber variable in exampe bean :"List special = new ArrayList();". And this member variable was not annotatet with OntToMany or somtehing else. What is the situation? What will be persisted? –  Mar 31 '14 at 07:23
  • So "SpecialBean" is an Entity? If its a relationship then you have to put the relation annotation ONeToMany/OneToOne/ManyToMany (though some JPA implementations can handle it without that too, DataNucleus JPA doesn't need it IIRC). Column is *still* not needed there. – Neil Stockton Mar 31 '14 at 10:59
  • can you please include a reference for your answer? – MozenRath Dec 05 '19 at 18:08
  • @MozenRath Here's your reference - https://docs.oracle.com/javaee/7/api/javax/persistence/Column.html – aathif Jun 04 '20 at 05:45