无法使用hibernate + springboot + jpa在My-SQL-DB中添加记录

时间:2017-04-10 09:15:43

标签: java mysql spring hibernate jpa

我正在尝试在我的spring启动应用程序中添加db中的记录,即使用hibernate + jpa。

我的模特课:

@Entity
@Table(name="branchpercentage")
public class BranchPercentage{

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Integer id;

    @Column(name="percentage")
    private Integer percentage;

    @OneToOne
    @JoinColumn(name="branch")
    private Branch branch;

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public Integer getPercentage() {
        return percentage;
    }
    public void setPercentage(Integer percentage) {
        this.percentage = percentage;
    }
    public Branch getBranch() {
        return branch;
    }
    public void setBranch(Branch branch) {
        this.branch = branch;
    }

}

控制器:

@RequestMapping("/report")
public ModelAndView home() throws SQLException{
    //for new table record
    Collection<Branch> branchList=branchServiceV1.getBranches();
    BranchComparator branchComp = new BranchComparator();
    List<Branch> list = new ArrayList<Branch>(branchList);
    Collections.sort(list, branchComp);
    branchList=list;
    BranchPercentage b;
    for(Branch branch:branchList){
        b=new BranchPercentage();
        b.setBranch(branch);
        b.setPercentage(5);
        prcntRepository.save(b);
    }


    ModelAndView modelAndView = new ModelAndView();
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    User user = userService.findUserByEmail(auth.getName());
    modelAndView.addObject("userName",user.getName() + " " + user.getLastName() + " (" + user.getEmail() + ")");
    modelAndView.addObject("adminMessage","Content Available Only for Users with Admin Role");
    modelAndView.setViewName("admin/report");
    return modelAndView;
}

存储库:

public interface PrcntRepository  extends CrudRepository<BranchPercentage, Integer> {

    public Collection<BranchPercentage> findById(BranchPercentage id);
    @SuppressWarnings("unchecked")
    BranchPercentage save (BranchPercentage obj);
}

属性文件:

  

spring.datasource.url = jdbc:mysql:// localhost:3306 / orderonline

     

spring.datasource.username = root spring.datasource.password = root

     

spring.datasource.testWhileIdle = true

     

spring.datasource.validationQuery = SELECT spring.jpa.show-sql = true

     

spring.jpa.hibernate.ddl-auto = update

     

spring.jpa.hibernate.naming-strategy =   org.hibernate.cfg.ImprovedNamingStrategy

     

spring.jpa.properties.hibernate.dialect =   org.hibernate.dialect.MySQL5Dialect

Branch.java:
@SuppressWarnings("serial")
@Entity
@Table(name="branch")
public class Branch implements Serializable {


    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id")
    private Integer id;
    @Column(name="name")
    private String name;    
    @Column(name="town")
    private String town;    
    @Column(name="city")
    private String city;    
    @Column(name="county")
    private String county;

        /* setter and getters */
}

但是我得到了这个错误

引起:org.hibernate.exception.ConstraintViolationException:无法执行语句

引起:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:无法添加或更新子行:外键约束失败(o rderonlinebranchpercentage,CONSTRAINT FK8pxwopwdv21hixfxcapuynnlq外键(branch)参考branchid))

知道我哪里做错了......?

0 个答案:

没有答案