2

I have a simple hibernate sample that have an entity class. I add a field to entity class that i don't want to create a column for that field in table of database. how can i do that?

my Entity class:

@Entity
@Table(name = "person")
public class PersonEntity {

private Long id;
private String emailEntity;
private String nameEntity;
private String familyEntity;
private String cityEntity;
private String phoneEntity;
private Long orgId;
private String province;    

@Id
@GeneratedValue
public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

@Column(name = "email")
public String getEmailEntity() {
    return emailEntity;
}

public void setEmailEntity(String emailEntity) {
    this.emailEntity = emailEntity;
}

@Column(name = "name")
public String getNameEntity() {
    return nameEntity;
}

public void setNameEntity(String nameEntity) {
    this.nameEntity = nameEntity;
}

@Column(name = "last_name")
public String getFamilyEntity() {
    return familyEntity;
}

public void setFamilyEntity(String familyEntity) {
    this.familyEntity = familyEntity;
}

@Column(name = "city")
public String getCityEntity() {
    return cityEntity;
}

public void setCityEntity(String cityEntity) {
    this.cityEntity = cityEntity;
}

@Column(name = "phone")
public String getPhoneEntity() {
    return phoneEntity;
}

public void setPhoneEntity(String phoneEntity) {
    this.phoneEntity = phoneEntity;
}

@Column(name = "org_id")
public Long getOrgId() {
    return orgId;
}

public void setOrgId(Long orgId) {
    this.orgId = orgId;
}

public String getProvince() {
    return province;
}

public void setProvince(String province) {
    this.province = province;
}
}

hibernate.cfg.xml:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
    <!-- Database connection settings -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/emailDB</property>
    <property name="connection.username">root</property>
    <property name="connection.password">123</property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>

    <!-- Disable the second-level cache  -->
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">false</property>

    <property name="hibernate.hbm2ddl.auto">update</property>
    <!--<mapping class="net.viralpatel.hibernate.Employee"/>-->
    <!--<mapping class="net.viralpatel.hibernate.PersonEntity"/>-->
    <mapping class="organizationsTab.PersonEntity"/>
    <mapping class="organizationsTab.OrgEntity"/>


</session-factory>
</hibernate-configuration>

'province' field is an auxiliary field and I don't want to create a column for that in table. how can i do that?

hossein
  • 71
  • 2
  • 7
  • not sure with mysql but there should be some property like IgnoreDataMember or ignore.. these keywords works for other languages. – Anupam Sharma Jan 21 '15 at 06:13
  • possible duplicate of [how to make hibernate ignore class variables that are not mapped!](http://stackoverflow.com/questions/4662582/how-to-make-hibernate-ignore-class-variables-that-are-not-mapped) – gknicker Jan 21 '15 at 06:18
  • `@Transient` should do it. Try with `create-drop` in **test** environment. – Rohit Jan 21 '15 at 07:00

2 Answers2

2

Annotate the field with @Transient annotation See more here

StanislavL
  • 56,971
  • 9
  • 68
  • 98
0

Every non static non transient property (field or method depending on the access type) of an entity is considered persistent, unless you annotate it as @Transient.

public transient int counter; //transient property

private String firstname; //persistent property

So you can use @Transient annotation on the property you do not wish to create a column for in database.

Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116