spring-数据-jdbc 只读
spring-data-jdbc readOnly
我正在尝试使某些字段 readOnly -> insert 和 update aka save() 不应将该字段发送到数据库,但应使用 select 填充该字段。
@ReadOnlyProperty 来自 org.springframework.data.annotation.ReadOnlyProperty 并不能解决问题。
版本: spring-引导:2.2.0.RC1,spring-数据-jdbc:1.1.0.RELEASE, spring-data-commons: 2.2.0.RELEASE
db: MSSQL
它应该有效吗?还有其他方法吗?
注意:请不要将 spring-data-jdbc 与 spring-data-jpa
混用
import java.util.Set;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.ReadOnlyProperty;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.MappedCollection;
public class Organization {
@Id
private Long id;
private String name;
@Column("readOnlyProperty")
@ReadOnlyProperty
private String readOnlyProperty;
@ReadOnlyProperty
@MappedCollection
private Set<Employee> employees;
}
import org.springframework.data.annotation.Id;
public class Employee {
@Id
private Long id;
private String name;
}
@Test
public void insert() {
// insert should not set readOnlyProperty
Organization organization = new Organization("org1", "readOnly");
Employee employee = new Employee("emp1");
Set<Employee> employess = new HashSet<>();
employess.add(employee);
organization.setEmployees(employess);
organizationRepository.save(organization);
}
日志:
执行准备好的 SQL 语句 [INSERT INTO organization (name, readOnlyProperty) VALUES (?, ?)]
正在执行准备好的 SQL 语句 [INSERT INTO employee (name, organization) VALUES (?, ?)]
我没有测试,但是根据this
The Column annotation and XML element defines insertable and updatable options. These allow for this column, or foreign key field to be omitted from the SQL INSERT or UPDATE statement. These can be used if constraints on the table prevent insert or update operations. They can also be used if multiple attributes map to the same database column, such as with a foreign key field through a ManyToOne and Id or Basic mapping. Setting both insertable and updatable to false, effectively mark the attribute as read-only.
@Column(name="COLUMN_NAME",updatable=false, insertable=false)
private String fieldName;
应将字段设置为只读。
这是一个错误。
我为它创建了 DATAJDBC-431,它可能会在下一个服务版本中修复。
我正在尝试使某些字段 readOnly -> insert 和 update aka save() 不应将该字段发送到数据库,但应使用 select 填充该字段。
@ReadOnlyProperty 来自 org.springframework.data.annotation.ReadOnlyProperty 并不能解决问题。
版本: spring-引导:2.2.0.RC1,spring-数据-jdbc:1.1.0.RELEASE, spring-data-commons: 2.2.0.RELEASE
db: MSSQL
它应该有效吗?还有其他方法吗?
注意:请不要将 spring-data-jdbc 与 spring-data-jpa
混用import java.util.Set;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.ReadOnlyProperty;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.MappedCollection;
public class Organization {
@Id
private Long id;
private String name;
@Column("readOnlyProperty")
@ReadOnlyProperty
private String readOnlyProperty;
@ReadOnlyProperty
@MappedCollection
private Set<Employee> employees;
}
import org.springframework.data.annotation.Id;
public class Employee {
@Id
private Long id;
private String name;
}
@Test
public void insert() {
// insert should not set readOnlyProperty
Organization organization = new Organization("org1", "readOnly");
Employee employee = new Employee("emp1");
Set<Employee> employess = new HashSet<>();
employess.add(employee);
organization.setEmployees(employess);
organizationRepository.save(organization);
}
日志: 执行准备好的 SQL 语句 [INSERT INTO organization (name, readOnlyProperty) VALUES (?, ?)]
正在执行准备好的 SQL 语句 [INSERT INTO employee (name, organization) VALUES (?, ?)]
我没有测试,但是根据this
The Column annotation and XML element defines insertable and updatable options. These allow for this column, or foreign key field to be omitted from the SQL INSERT or UPDATE statement. These can be used if constraints on the table prevent insert or update operations. They can also be used if multiple attributes map to the same database column, such as with a foreign key field through a ManyToOne and Id or Basic mapping. Setting both insertable and updatable to false, effectively mark the attribute as read-only.
@Column(name="COLUMN_NAME",updatable=false, insertable=false)
private String fieldName;
应将字段设置为只读。
这是一个错误。 我为它创建了 DATAJDBC-431,它可能会在下一个服务版本中修复。