I have a table tbl_sky
that has 2 properties name
and model
and I would use Hibernate annotation like;
@Entity @Table(name="tbl_sky") public class Sky implements Serializable { private String name; private String model; private String status; @Id public String getName() { return name; } . . .
But I need to add one more property status
that does not exist in the table but is needed in the class. How could I declare that property so that I have it in my class but not in my db-table?
All help is appreciated.
In hibernate, the preffered way to do this is by annotations. Annotations are piece of code that eases our work in mapping classes with tables, and methods with coulmns. Also, rows of tables to the Objects of the classes. This is also possible in XML mapping, but I've used annotations more.
The entity class must be annotated with the Entity annotation or denoted in the XML descriptor as an entity. So, if you use annotations for mappings, @Entity is mandated by the specification and Hibernate has to adhere to it.
@Entity annotation marks this class as an entity. @Table annotation specifies the table name where data of this entity is to be persisted. If you don't use @Table annotation, hibernate will use the class name as the table name by default. @Id annotation marks the identifier for this entity.
Use @Transient
annotation for field you are not going to store in DB:
@Transient public String getStatus() { return status; }
or:
@Transient private String status;
Mark it as @Transient
, and it won't be part of the DB schema.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With