英文:
Make certain fields in entity not be saved to the database
问题
我正在学习Spring和Hibernate。我已经准备好了一个图像实体。字段已成功保存到数据库作为列名,但我需要使实体中的某些属性存在但不保存到数据库。但实际上它们却被保存了。我尝试移除@Column
注解或使用@Column(insertable=false, updatable=false)
,但没有成功。数据库中始终有带有空值的列。那么如何阻止Spring和Hibernate创建特定字段的列。我正在尝试对size
、width
和height
字段进行操作。
实体的代码:
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 = "images")
public class Image {
@Id
@SequenceGenerator(name = "mySeqGen6", sequenceName = "mySeq6", initialValue = 11, allocationSize = 100)
@GeneratedValue(generator = "mySeqGen6")
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="image_category_id")
private ImageCategory imageCategory;
@Column(name="datecreated", columnDefinition="TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
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 "Post{" +
"id=" + id +
", title='" + title + '\'' +
", url='" + url + '\'' +
", date=" + dateCreated +
'}';
}
@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
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>tk.trzczy</groupId>
<artifactId>spring-webmvc</artifactId>
<version>0.0.1</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<aspect.version>1.9.2</aspect.version>
<jackson.version>2.9.8</jackson.version>
<hibernate.version>5.4.2.Final</hibernate.version>
<hibernate.validator.version>5.2.1.Final</hibernate.validator.version>
<c3p0.version>0.9.5.2</c3p0.version>
</properties>
<dependencies>
<!-- servlets and jps -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-extras</artifactId>
<version>3.0.8</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</exclusion>
<exclusion>
<groupId>jboss</groupId>
<artifactId>javassist</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<!--hibernate-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
<exclusions>
<exclusion>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- Hibernate-C3P0 Integration -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>${c3p0.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate.validator.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
<!-- for rest services -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
<!-- For Aop -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspect.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspect.version}</version>
</dependency>
<!-- To Send Email -->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<!--logging-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-web -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.30</version>
</dependency>
<!-- PostgreSQL -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.16</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-taglibs -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>5.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-war-plugin -->
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-surefire-plugin -->
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-rest-core -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-core</artifactId>
<version>3.3.4.RELEASE</version>
</dependency>
</dependencies>
<build>
<!-- To define the plugin version in your parent POM -->
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<!-- To use the plugin goals in your POM or parent POM -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
答案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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论