Spring-Data-JPA 一对多关联保存,关联外键字段为NUll

今天遇到一个问题,在使用Spring Data JPA一对多关联时候保存到数据库,数据都保存了,但是外键的值为空!

原因:实体类配置关联关系后还要在保存的时候体现出关联性。

实体类:

package com.zzg.entity;

import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

@Entity
@Table(name = "author")
@Data // 自动生成get、set、toString、equals方法
@AllArgsConstructor // 全参构造方法
@NoArgsConstructor // 无参构造方法
@Accessors(chain = true) // 链式编程
public class Author {
	@Id // 主键
    @GeneratedValue(strategy = GenerationType.IDENTITY) // 自增长策略
    private Long id; //id
   
    @Column(nullable = false, length = 20)
    private String name;//姓名
    
    //@JoinColumn(name = "author_id")
    //级联保存、更新、删除、刷新;延迟加载。当删除用户,会级联删除该用户的所有文章
    //拥有mappedBy注解的实体类为关系被维护端
     //mappedBy="author"中的author是Article中的author属性
    @OneToMany(mappedBy = "author",cascade=CascadeType.ALL,fetch=FetchType.EAGER)
    private List
articleList;//文章列表 }
package com.zzg.entity;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;

@Entity
@Table(name = "article")
@Setter
@Getter
@EqualsAndHashCode// 自动生成get、set、toString、equals方法
@AllArgsConstructor // 全参构造方法
@NoArgsConstructor // 无参构造方法
@Accessors(chain = true) // 链式编程
public class Article {
	    @Id
	    @GeneratedValue(strategy = GenerationType.IDENTITY) // 自增长策略
	    private Long id;
	   
	    
	    @Column(nullable = false, length = 50) // 映射为字段,值不能为空
	    private String title;
	   
	    @Column(nullable = false) // 映射为字段,值不能为空
	    private String content;//文章全文内容
	    
	    //可选属性optional=false,表示author不能为空。删除文章,不影响用户
	    @ManyToOne(cascade={CascadeType.MERGE,CascadeType.REFRESH},optional=false)
	    @JoinColumn(name="author_id")//设置在article表中的关联字段(外键)
	    private Author author;//所属作者

		@Override
		public String toString() {
			// TODO Auto-generated method stub
			return "Article{ id='"+id+"'}";
		}    
}

测试功能代码:

@Test
	public void insertOneToMany(){
		Author author = new Author();
		author.setName("周晨曦");
		
		
		List
list = new ArrayList
(); for(int i =0; i<10; i++ ){ Article article = new Article(); article.setTitle("标题:" + i); article.setContent("内容:" + i); // 记住一定要添加关系实体对象,否在关联字段为null article.setAuthor(author); list.add(article); } author.setArticleList(list); authorRepository.save(author); }

 

你可能感兴趣的:(spring,Data,JPA)