java - 如何在mysql数据库中创建表时忽略实体类中的字段?

标签 java hibernate jpa annotations

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

我的实体类:

@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>

“省”字段是辅助字段,我不想在表中为其创建列。我该怎么做?

最佳答案

使用@Transient注解对字段进行注解查看更多here

关于java - 如何在mysql数据库中创建表时忽略实体类中的字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28060637/

相关文章:

java - JPA 1.0 Hibernate & Derby HashMap 与 Enum 键用法

java - Spring Data 更新多对一属性

java - 声明后Java中的数组大小是固定的?

java - hibernate 和日期类型 : length attribute

java - DAO 和 Spring Autowired

java - hibernate spring简单单元测试读取数据。 findAll() 无法引用

java - 在单独的表中映射 @Embeddable

java - 在 Java 中使用反射迭代 Nd 数组的问题

java - 未调用actionListener中的方法

java - 数据源的 Spring Boot 自动配置