使实体中的特定字段不会被保存到数据库中。

huangapple go评论98阅读模式
英文:

Make certain fields in entity not be saved to the database

问题

我正在学习Spring和Hibernate。我已经准备好了一个图像实体。字段已成功保存到数据库作为列名,但我需要使实体中的某些属性存在但不保存到数据库。但实际上它们却被保存了。我尝试移除@Column注解或使用@Column(insertable=false, updatable=false),但没有成功。数据库中始终有带有空值的列。那么如何阻止Spring和Hibernate创建特定字段的列。我正在尝试对sizewidthheight字段进行操作。

实体的代码:

package tk.trzczy.gallery.domain;

import javax.persistence.*;
import java.awt.*;
import java.util.Date;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;

@Entity
@Table(name = "images")
public class Image {
    // ...
    @Column(insertable=false, updatable=false)
    private Long width;

    @Column(insertable=false, updatable=false)
    private Long height;

    @Column(insertable=false, updatable=false)
    private Dimension size;

    // ...
}

pom.xml

<!-- 代码太长,省略 -->
英文:

I am learning Spring and Hibernate. I prepared the entity of an image. The fields are succesfullu saved to the database as column names, but I need that some of properties to exist in the entity but not be saved to database. But the actually are being saved. I tried to remove a @Column annotation or use @Column(insertable=false, updatable=false) but not succeed. Always got the columns in the database with null value. So how to prevent Spring and Hibernate from create columns of certain fields. I am trying to do it with the fields size, width and height.

Code of the entity:

package tk.trzczy.gallery.domain;

import javax.persistence.*;
import java.awt.*;
import java.util.Date;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;

@Entity
@Table(name = &quot;images&quot;)
public class Image {
    @Id
    @SequenceGenerator(name = &quot;mySeqGen6&quot;, sequenceName = &quot;mySeq6&quot;, initialValue = 11, allocationSize = 100)
    @GeneratedValue(generator = &quot;mySeqGen6&quot;)
    private Integer id;

    @Column(nullable = false, length = 300)
    private String title;

    @Column(nullable = false)
    private String url;

    @Column(insertable=false, updatable=false)
    private Long width;

    @Column(insertable=false, updatable=false)
    private Long height;

    @Column(insertable=false, updatable=false)
    private Dimension size;

    @ManyToOne(cascade=CascadeType.PERSIST,fetch=FetchType.EAGER)
    @JoinColumn(name=&quot;image_category_id&quot;)
    private ImageCategory imageCategory;

    @Column(name=&quot;datecreated&quot;, columnDefinition=&quot;TIMESTAMP DEFAULT CURRENT_TIMESTAMP&quot;)
    private Date dateCreated;

    @PrePersist
    protected void onCreate() {
        dateCreated = new Date();
    }

    @JsonIgnore
    public Integer getId() {
        return id;
    }

    @JsonProperty
    public void setId(Integer id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }

    @JsonIgnore
    public Date getDateCreated() {
        return dateCreated;
    }

    @JsonProperty
    public void setDateCreated(Date dateCreated) {
        this.dateCreated = dateCreated;
    }

    public Image() {}

    public Image(Integer id, String title, String url) {
        this.id = id;
        this.title = title;
        this.url = url;
    }

    @Override
    public String toString() {
        return &quot;Post{&quot; +
                &quot;id=&quot; + id +
                &quot;, title=&#39;&quot; + title + &#39;\&#39;&#39; +
                &quot;, url=&#39;&quot; + url + &#39;\&#39;&#39; +
                &quot;, date=&quot; + dateCreated +
                &#39;}&#39;;
    }

    @JsonIgnore
    public ImageCategory getImageCategory() {
        return imageCategory;
    }

    @JsonProperty
    public void setImageCategory(ImageCategory imageCategory) {
        this.imageCategory = imageCategory;
    }

    public long getWidth() {
        return width;
    }

    public void setWidth(long width) {
        this.width = width;
    }

    public long getHeight() {
        return height;
    }

    public void setHeight(long height) {
        this.height = height;
    }

    public Dimension getSize() {
        return size;
    }

    public void setSize(Dimension size) {
        this.size=size;
    }
}

pom.xml

&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;groupId&gt;tk.trzczy&lt;/groupId&gt;
&lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
&lt;version&gt;0.0.1&lt;/version&gt;
&lt;packaging&gt;war&lt;/packaging&gt;
&lt;properties&gt;
&lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
&lt;aspect.version&gt;1.9.2&lt;/aspect.version&gt;
&lt;jackson.version&gt;2.9.8&lt;/jackson.version&gt;
&lt;hibernate.version&gt;5.4.2.Final&lt;/hibernate.version&gt;
&lt;hibernate.validator.version&gt;5.2.1.Final&lt;/hibernate.validator.version&gt;
&lt;c3p0.version&gt;0.9.5.2&lt;/c3p0.version&gt;
&lt;/properties&gt;
&lt;dependencies&gt;
&lt;!-- servlets and jps --&gt;
&lt;dependency&gt;
&lt;groupId&gt;javax.servlet&lt;/groupId&gt;
&lt;artifactId&gt;jstl&lt;/artifactId&gt;
&lt;version&gt;1.2&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;javax.servlet&lt;/groupId&gt;
&lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt;
&lt;version&gt;4.0.1&lt;/version&gt;
&lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tiles&lt;/groupId&gt;
&lt;artifactId&gt;tiles-extras&lt;/artifactId&gt;
&lt;version&gt;3.0.8&lt;/version&gt;
&lt;exclusions&gt;
&lt;exclusion&gt;
&lt;groupId&gt;org.slf4j&lt;/groupId&gt;
&lt;artifactId&gt;jcl-over-slf4j&lt;/artifactId&gt;
&lt;/exclusion&gt;
&lt;exclusion&gt;
&lt;groupId&gt;jboss&lt;/groupId&gt;
&lt;artifactId&gt;javassist&lt;/artifactId&gt;
&lt;/exclusion&gt;
&lt;/exclusions&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;commons-fileupload&lt;/groupId&gt;
&lt;artifactId&gt;commons-fileupload&lt;/artifactId&gt;
&lt;version&gt;1.3.3&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
&lt;artifactId&gt;commons-io&lt;/artifactId&gt;
&lt;version&gt;1.3.2&lt;/version&gt;
&lt;/dependency&gt;
&lt;!--hibernate--&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.hibernate&lt;/groupId&gt;
&lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
&lt;version&gt;${hibernate.version}&lt;/version&gt;
&lt;exclusions&gt;
&lt;exclusion&gt;
&lt;groupId&gt;org.javassist&lt;/groupId&gt;
&lt;artifactId&gt;javassist&lt;/artifactId&gt;
&lt;/exclusion&gt;
&lt;/exclusions&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.hibernate&lt;/groupId&gt;
&lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
&lt;version&gt;${hibernate.version}&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- Hibernate-C3P0 Integration --&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.hibernate&lt;/groupId&gt;
&lt;artifactId&gt;hibernate-c3p0&lt;/artifactId&gt;
&lt;version&gt;${hibernate.version}&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- c3p0 --&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.mchange&lt;/groupId&gt;
&lt;artifactId&gt;c3p0&lt;/artifactId&gt;
&lt;version&gt;${c3p0.version}&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.hibernate&lt;/groupId&gt;
&lt;artifactId&gt;hibernate-validator&lt;/artifactId&gt;
&lt;version&gt;${hibernate.validator.version}&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;mysql&lt;/groupId&gt;
&lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
&lt;version&gt;5.1.34&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- for rest services --&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
&lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
&lt;version&gt;${jackson.version}&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;javax.servlet.jsp&lt;/groupId&gt;
&lt;artifactId&gt;javax.servlet.jsp-api&lt;/artifactId&gt;
&lt;version&gt;2.3.3&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- For Aop --&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.aspectj&lt;/groupId&gt;
&lt;artifactId&gt;aspectjrt&lt;/artifactId&gt;
&lt;version&gt;${aspect.version}&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.aspectj&lt;/groupId&gt;
&lt;artifactId&gt;aspectjweaver&lt;/artifactId&gt;
&lt;version&gt;${aspect.version}&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- To Send Email --&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.sun.mail&lt;/groupId&gt;
&lt;artifactId&gt;javax.mail&lt;/artifactId&gt;
&lt;version&gt;1.5.4&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
&lt;artifactId&gt;spring-security-core&lt;/artifactId&gt;
&lt;version&gt;5.2.0.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework&lt;/groupId&gt;
&lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
&lt;version&gt;5.2.0.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework&lt;/groupId&gt;
&lt;artifactId&gt;spring-tx&lt;/artifactId&gt;
&lt;version&gt;5.2.0.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework&lt;/groupId&gt;
&lt;artifactId&gt;spring-orm&lt;/artifactId&gt;
&lt;version&gt;5.2.0.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
&lt;artifactId&gt;spring-security-web&lt;/artifactId&gt;
&lt;version&gt;5.2.0.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
&lt;artifactId&gt;spring-security-config&lt;/artifactId&gt;
&lt;version&gt;5.2.0.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;!--logging--&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
&lt;artifactId&gt;log4j-core&lt;/artifactId&gt;
&lt;version&gt;2.8.2&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
&lt;artifactId&gt;log4j-api&lt;/artifactId&gt;
&lt;version&gt;2.8.2&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-web --&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
&lt;artifactId&gt;log4j-web&lt;/artifactId&gt;
&lt;version&gt;2.13.3&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.slf4j&lt;/groupId&gt;
&lt;artifactId&gt;slf4j-simple&lt;/artifactId&gt;
&lt;version&gt;1.7.30&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- PostgreSQL --&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.postgresql&lt;/groupId&gt;
&lt;artifactId&gt;postgresql&lt;/artifactId&gt;
&lt;version&gt;42.2.16&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework&lt;/groupId&gt;
&lt;artifactId&gt;spring-context-support&lt;/artifactId&gt;
&lt;version&gt;5.1.3.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-taglibs --&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
&lt;artifactId&gt;spring-security-taglibs&lt;/artifactId&gt;
&lt;version&gt;5.1.2.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework&lt;/groupId&gt;
&lt;artifactId&gt;spring-context&lt;/artifactId&gt;
&lt;version&gt;5.2.7.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-war-plugin --&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
&lt;version&gt;3.2.2&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-surefire-plugin --&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
&lt;version&gt;2.22.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework&lt;/groupId&gt;
&lt;artifactId&gt;spring-web&lt;/artifactId&gt;
&lt;version&gt;5.2.0.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-rest-core --&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
&lt;artifactId&gt;spring-data-rest-core&lt;/artifactId&gt;
&lt;version&gt;3.3.4.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;build&gt;
&lt;!-- To define the plugin version in your parent POM --&gt;
&lt;pluginManagement&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
&lt;version&gt;2.22.1&lt;/version&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/pluginManagement&gt;
&lt;plugins&gt;
&lt;!-- To use the plugin goals in your POM or parent POM --&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
&lt;configuration&gt;
&lt;source&gt;1.8&lt;/source&gt;
&lt;target&gt;1.8&lt;/target&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
&lt;/project&gt;

答案1

得分: 5

transient是Java的一个关键字,用于标记成员变量,在持久化为字节流时不会被序列化。

在JPA或Hibernate中,@Transient注解用于指示一个字段不会被持久化,或者忽略要保存在数据库中的字段。@Transient存在于javax.persistence包中。它用于注解实体类、映射的超类或可嵌入类的属性或字段。

因此将该字段标记为@Transient

英文:

transient is a Java keyword which marks a member variable not to be serialized when it is persisted to streams of bytes.

@Transient annotation in JPA or Hibernate is used to indicate that a field is not to be persisted or ignore fields to save in the database. @Transient exist in javax. persistence package. It is used to annotate a property or field of an entity class, mapped superclass, or embeddable class.

hence mark the Field as @Transient.

huangapple
  • 本文由 发表于 2020年10月11日 21:15:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64304416.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定