【问题标题】:how to ignore a field in entity class for create table in mysql database?如何忽略实体类中的字段以在 mysql 数据库中创建表?
【发布时间】:2015-03-19 14:00:57
【问题描述】:

我有一个简单的休眠示例,它有一个实体类。我向实体类添加了一个字段,我不想在数据库表中为该字段创建列。我该怎么做?

我的实体类:

@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' 字段是一个辅助字段,我不想在表中为它创建一个列。我该怎么做?

【问题讨论】:

  • 不确定 mysql 但应该有一些属性,如 IgnoreDataMember 或 ignore.. 这些关键字适用于其他语言。
  • @Transient 应该这样做。在 test 环境中尝试&lt;property name="hibernate.hbm2ddl.auto"&gt;create-drop&lt;/property&gt;

标签: java hibernate jpa annotations


【解决方案1】:

用@Transient注解注解字段查看更多here

【讨论】:

  • 我使用了这个注解但是不能正常工作;为此在表中创建了一个空列
【解决方案2】:

实体的每个非静态非瞬态属性(字段或方法取决于访问类型)都被视为persistent,除非您将其注释为@Transient

public transient int counter; //transient property

private String firstname; //persistent property

因此,您可以在不希望在数据库中为其创建列的属性上使用 @Transient 注释。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-05
    • 1970-01-01
    • 2019-08-19
    • 2021-09-02
    • 2014-12-24
    • 1970-01-01
    • 2019-02-05
    • 2017-06-02
    相关资源
    最近更新 更多