如何使用Hibernate JPA批注仅创建一条记录

时间:2019-02-12 23:06:10

标签: java spring hibernate spring-boot jpa

我有3个实体。它们是:Tweet,线程和媒体。我正在使用Hibernate JPA注释。当我保存一个Thread对象时,它必须触发到tweet并自动像线程一样保存tweet,当然也必须保存Tweet的“ media”。

您可以看到我的实体类,如下所示。现在,我正在研究Thread之间的Tweet。当我使用这些代码时,正在记录的线程数与tweet对象一样多。但是我只想在线程内一个记录

保存线程后,过程必须与之类似:

  1. 将新记录添加到线程表。 (仅一行)
  2. 将所有tweet添加到tweet表(“ threadid”列的所有相同,因为这些tweet仅属于一个线程!)
  3. 将所有媒体添加到媒体表(“ tweetid”列的所有相同

推文表:

@Entity
@Table(name = "tbl_tweet")
public class Tweet implements Serializable {

    @Id
    @GeneratedValue
    private long id;
    @Column(name = "tweetid")
    private String tweetID;
    private String parentTweetID;
    private String avatar;
    private String owner_name;
    private String owner_nick;
    private String content;
    private String sent_time;
    private String sent_date;
    private String retweet_count;
    private String like_count;
    private String owner_link;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name="threadid")
    private Thread thread;

    @OneToMany(mappedBy = "tweet",fetch = FetchType.LAZY,cascade = {CascadeType.PERSIST,CascadeType.MERGE})
    private List<Media> mediaURLs;

线程表:

@Entity
@Table(name = "tbl_thread")
public class Thread implements Serializable {
        @Id
        @GeneratedValue
        private Long id;
        @Column(name = "threadid")
        private String threadID;

        @Transient
        private List<Tweet> listOfTweets;

媒体表:

@Entity
@Table(name = "tbl_media")
public class Media implements Serializable {
    @Id
    @GeneratedValue
    private long id;
    private String mediaID;
    private String mediaType;
    private String mediaUrl;
    private String mediaUrlHttps;
    private String mediaVideoUrl;

    @ManyToOne
    @JoinColumn(name = "tweetid")
    private Tweet tweet;

我的预期和实际结果: http://prntscr.com/mkaul5

1 个答案:

答案 0 :(得分:0)

我改变了方式。目前运作良好。注释万岁!

推文表:

pipe=Pipeline([('Vect', CountVectorizer()), ('rf', RandomForestRegressor(random_state=1)) ])

X_train, X_test, y_train, y_test=train_test_split(df['X'], df['price'], shuffle= False, test_size=0.5)

reg=pipe.fit(X_train,y_train )
mypred= reg.predict(X_test)
r2_score(mypred, y_test)
# result is -0.2
cross_val_score(pipe,df['X'], df['price'],cv=2)
# result is about 0.3

线程表:

@Entity
@Table(name = "tbl_tweet")
public class Tweet implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    @Column(unique = true,nullable = false,name = "tweetID")
    private String tweetID;
    private String parentTweetID;
    private String avatar;
    private long owner_id;
    private String owner_name;
    private String owner_nick;
    private String content;
    private String sent_time;
    private String sent_date;
    private String retweet_count;
    private String like_count;
    @Transient
    private String quotedTweetHtml;
    @Transient
    @JoinColumn(name = "threadid",referencedColumnName = "id")
    private Thread thread;


    @OneToMany(cascade = CascadeType.PERSIST,fetch = FetchType.EAGER)
    @JoinColumn(name = "tweetID")
    private List<Media> mediaURLs;

媒体表:

@Entity
@Table(name = "tbl_thread")
public class Thread implements Serializable {
        @Id
        @GeneratedValue
        private Long id;
        @Column(name = "threadid")
        private String threadID;
        @OneToMany(cascade = CascadeType.PERSIST,fetch = FetchType.EAGER)
        @JoinColumn(name = "threadid")
        private List<Tweet> listOfTweets;