-1

我将数据发送到服务器阿贾克斯后JPA一对多的关系 - 外键未设置

var obj = { 
    'sub': 'this is a test.' 
    ,'userName': 'dbdyd' 
    ,'saveDate': new Date() 
    ,'comments': [ 
     {'comment':'1a', 'saveDate2': new Date(), 'seq2': null} 
     ,{'comment':'2b', 'saveDate2': new Date(), 'seq2': null} 
    ] 
    }; 


    $.ajax({ 
    url: '/cp/RestApi/Posts', 
    type: 'POST', 
    dataType: 'json', 
    contentType: 'application/json', 
    data: JSON.stringify(obj) 
    }) 
    .done(function() { 
    console.log("success"); 
    }) 
    .fail(function() { 
    console.log("error"); 
    }); 

我遇到了服务器错误。 foreignKey未设置为注释表。

INFO: Starting Coyote HTTP/1.1 on http-8080 
    Posts [seq=null, sub=this is a test., userName=dbdyd, saveDate=Sun Mar 13 09:05:46 KST 2016 
, comments=[Comments [seq2=null, comment=2b, saveDate2=Sun Mar 13 09:05:46 KST 2016, posts=null], Comments [seq2=null, comment=1a, saveDate2=Sun Mar 13 09:05:46 KST 2016, posts=null]]] 
Hibernate: insert into onnuricp.posts (save_date, sub, user_name) values (?, ?, ?) 
Hibernate: insert into onnuricp.comments (comment, seq, save_date2) values (?, ?, ?) 
09:05:47.315 [http-8080-1] ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Column 'seq' cannot be null 
Mar 13, 2016 9:05:47 AM org.apache.catalina.core.StandardWrapperValve invoke 

数据库架构

Create Table: CREATE TABLE `posts` (
    `seq` int(11) NOT NULL AUTO_INCREMENT COMMENT '게시판번호', 
    `sub` varchar(255) DEFAULT NULL COMMENT '제목', 
    `user_name` varchar(50) DEFAULT NULL COMMENT '작성자', 
    `save_date` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '입력일자', 
    PRIMARY KEY (`seq`) 
) ENGINE=InnoDB AUTO_INCREMENT=84 DEFAULT CHARSET=utf8 COMMENT='게시판' 



CREATE TABLE `comments` (
    `seq2` int(11) NOT NULL AUTO_INCREMENT COMMENT '댓글번호', 
    `comment` varchar(255) NOT NULL COMMENT '내용', 
    `save_date2` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '작성일자', 
    `seq` int(11) NOT NULL COMMENT '게시판번호', 
    PRIMARY KEY (`seq2`), 
    KEY `FK_posts_TO_comments` (`seq`), 
    CONSTRAINT `FK_posts_TO_comments` FOREIGN KEY (`seq`) REFERENCES `posts` (`seq`) ON DELETE CASCADE ON UPDATE CASCADE 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='댓글' 
; 

我有一些问题,与春天的数据JPA。 @OneToMany的关系似乎是错的,但我不知道它。

@Entity 
@Table(name = "posts") 
public class Posts { 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Basic(optional = false) 
    @Column(name = "seq") 
    private Integer seq; 

    @Column(name = "sub", nullable = true) 
    private String sub; 

    @Column(name = "user_name") 
    private String userName; 

    @Column(name = "save_date", nullable = false) 
    @Temporal(TemporalType.TIMESTAMP) 
    private Date saveDate; 

    @OneToMany(mappedBy = "posts", cascade = CascadeType.ALL) 
    private Set<Comments> comments; 

} 



@Entity 
@Table(name = "comments") 
public class Comments implements Serializable { 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Basic(optional = false) 
    @Column(name = "seq2") 
    private Integer seq2; 

    @Column(name = "comment", nullable = false) 
    private String comment; 

    @Column(name = "save_date2", nullable = false) 
    @Temporal(TemporalType.TIMESTAMP) 
    private Date saveDate2; 

    @ManyToOne(optional = false) 
    @JoinColumn(name = "seq", referencedColumnName="seq", nullable = false) 
    private Posts posts; 

} 

保存对象(帖子和评论)。 和这样的服务实现。

Posts entity = new Post(); 
Posts savedEntity = repository.save(entity); 

请帮助我与JPA Relationship。

回答

0

您的@OneToMany映射看起来正确。但你并不需要指定referencedColumnName = "seq"只使用

@ManyToOne(optional = false) 
@JoinColumn(name = "seq", nullable = false) 
private Posts posts; 

不要为CommentsPosts用复数。只需Comment,Post

可以使用Posts

Posts post = new Posts(); 
post.setComments(new HashSet<Comments>()); 

Comments comment = new Comments(); 
comment.setPosts(post); 
post.getComments().add(comment); 

save(post); 

节省Comments如果你已经在数据库中post。您可以通过这种方式

Comments comment = new Comments(); 
comment.setPosts(post); 

save(comment); 
+0

thans to @ v.ladynev非常适合您的礼貌答案! ~~。 –

+0

@永裕不客气。 –

0

我解决了这个问题,并跑进另一个问题添加comment。 所以,我为这篇文章添加了一些解决方案。

首先我修正了外键没有设置为Comments对象。 我必须添加方法[addComments(Comments co)]到Posts类。 如果我有数组注释对象,我必须添加它实现添加到集合的对象方法。没有什么可以添加另一个东

@Entity 
@Table(name = "posts", catalog = "onnuricp") 
public class Posts { 

    @OneToMany(mappedBy = "posts", cascade = CascadeType.ALL) 
    private Set<Comments> comments; 

    public Posts() { 
    comments = new HashSet<Comments>(); 
    } 

    public void addComments(Comments co) { 
    co.setPosts(this); 
    comments.add(co); 
    } 

} 

其次,我遇到了一个问题,关于杰克逊数据绑定错误。 添加一些代码来解决它。

@JsonIdentityInfo(
    generator = ObjectIdGenerators.PropertyGenerator.class, property = "seq2") 

public class CommentsDto implements Serializable { 

    getter .. ; 
    setter .. ; 

} 


@JsonIdentityInfo(
    generator = ObjectIdGenerators.PropertyGenerator.class, property = "seq") 

public class PostsDto implements Serializable { 

    getter .. ; 
    setter .. ; 
}