0

I'm trying to create a back-end forum using JPA to interact with the database.. The problem that I'm facing now is the relationships between entities. I have 3 of them (at least for now) but when I try to create the tables using the JPA tools, it doesn't create them all which makes me think that there's something wrong with the relationships. Can anyone help me?

@Entity
@Table(name="users")
public class UserModel {

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long user_id;
    private String name;
    private String email;
    @OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST,CascadeType.MERGE})
    private ArrayList<TopicoModel> myTopics = new ArrayList<TopicoModel>();

    @ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST,CascadeType.MERGE})
    private ArrayList<TopicoModel> myFavTopics = new ArrayList<TopicoModel>();

--

@Entity
@Table(name="topics")
public class TopicModel{

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long topic_id;

    @OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST,CascadeType.MERGE})
    private ArrayList<PostModel> comments;

    @ManyToOne(cascade = CascadeType.MERGE)
    private UserModel author;

--

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

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long post_id;

    private String message;
    private int likes = 0;
    private int dislikes = 0;
    @OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST,CascadeType.MERGE})
    private ArrayList<UserModel> agrees;
    @OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST,CascadeType.MERGE})
    private ArrayList<UserModel> thanks;

EDIT1: When I try to populate the db with some test data its gives me the following error:

Internal Exception: java.sql.SQLException: Field 'myTopics_TOPIC_ID' doesn't have a default value
Error Code: 1364
Call: INSERT INTO users_topics (myFavTopics_TOPIC_ID, UserModel_USER_ID) VALUES (?, ?)
    bind => [2 parameters bound]
Query: DataModifyQuery(name="topicos_favoritos" sql="INSERT INTO users_topics (myFavTopics_TOPIC_ID, UserModel_USER_ID) VALUES (?, ?)")

EDIT2: I already tried to add a default value but it gives me the same error but with "myFavTopics" variable. Here is the code to populate the db:

UserModel u1 = new UserModel();
u1.setEmail("asd@asd.pt");
u1.setName("A");

UserModel u2 = new UserModel();
u2.setEmail("qwer@qwer.pt");
u2.setName("B");

PostModel p1 = new PostModel("message 1", 0, 0);
PostModel p2 = new PostModel("message 2", 10, 0);
PostModel p3 = new PostModel("message 3", 0, 20);

ArrayList<PostModel> commentP1 = new ArrayList<PostModel>();
commentP1.add(p1);
commentP1.add(p3);

ArrayList<PostModel> commentP2 = new ArrayList<PostModel>();
commentP2.add(p2);


TopicModel t1 = new TopicModel(commentP1, u1);
TopicModel t2 = new TopicModel(commentP2, u2);

u1.addMyTopic(t1);
u2.addMyTopic(t2);

u1.addMyFavTopic(t2);


UserModel u3 = new UserModel();
u3.setEmail("yujy@yj.pt");
u3.setName("C");

JPAEntityManager factory = new JPAEntityManager();
EntityManager em = factory.getEntityManager();

em.getTransaction().begin();
em.persist(t1);
em.persist(t2);
em.persist(p1);
em.persist(p2);
em.persist(p3);
em.persist(u1);
em.persist(u2);
em.persist(u3);

em.getTransaction().commit();
Ins
  • 173
  • 1
  • 11
  • Please, add the exception log to your post – MaQuiNa1995 Jun 07 '17 at 16:08
  • Looks like your not setting anything for myTopics when your inserting into UserModel. – K.Nicholas Jun 07 '17 at 16:18
  • Refer https://stackoverflow.com/questions/26336520/java-sql-sqlexception-field-supplier-id-doesnt-have-a-default-value – harshavmb Jun 07 '17 at 16:23
  • 1. It must be List, not ArrayList (or better, Set). 2. you shouldn't use EAGER fetching, especially on lists. 3. bidirectional associations must have an inverse side, using mappedBy. 4. IDs should be of type Long, not long. – JB Nizet Jun 07 '17 at 19:10

0 Answers0