2017-09-19 27 views
1

我遇到了一个持久对象的小问题。以下是我的实体类看起来像的一个例子。如何仅保存在spring crudrepository中分配的属性?

@Entity 
public class Example(){ 
    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO) 
    private Integer id; 
    private int number; 
    private String sentence; 
/* No arg const, getters and setters omitted */ 

CrudRepository接口:

@Repository 
public interface ExampleRepository extends CrudRepository<Example, Integer> 
{} 

服务实现接口:

@Service 
public class ExampleService{ 
    @Autowired 
    public ExampleRepository exampleRepository; 
    public void save(Example example){ 
     exampleRespository.save(example) 
    } 
} 

内CommandLineRunner的:

Example example1 = new Example(); 
example1.sentence("Hello World!"); 
exampleService.save(example1); 

现在我遇到问题是即使我没有给属性号赋值,它仍然会持续为0.如何阻止该属性被赋值为0并使其为空?

回答

0

变化

private int number; 

private Integer number; 
0

上述方案是好的,但如果再次在插入查询看到你的保存功能,它插入所有3列,即使你只分配一次example1.sentence("Hello World!");

您可以使用@DynamicInsert(true)@DynamicUpdate(true)在实体级别, 这将触发查询为

insert into example(sentence) values('Hello World!'); 

这样的查询性能将提高

+0

谢谢你的提示。我会记住这一点。 – Saurin