Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9186031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:25:27+00:00 2026-06-17T19:25:27+00:00

I’m trying to persist a User-class, which contains 2 lists: one with users to

  • 0

I’m trying to persist a User-class, which contains 2 lists: one with users to see which are following that specific user, and one with users to see which users that specific user follows.

User.java:

@Entity
@XmlRootElement
@Table(name="Users")
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
    private String web;
    private String bio;
    @OneToMany(cascade=CascadeType.ALL)
    private Collection<User> followers = new ArrayList();
    @OneToMany(cascade=CascadeType.ALL)
    private Collection<User> following = new ArrayList();

I have a class named UserDAOJPA.java which creates and modifies the users:

@Alternative
@Stateless
public class UserDAOJPA implements Serializable, IUserDAO {

    @PersistenceContext(unitName = "KwetterSOAPPU")
    private EntityManager em;

    @Override
    public void create(User user) {
        em.persist(user);
    }
@Override
    public List<User> getFollowers(User user) {
        List<User> followers;
        followers = (List<User>) user.getFollowers();
        return followers;
    }

    @Override
    public void addFollower(User user, User follower)
    {
        user.addFollower(follower);
        em.merge(user);
    }

    @Override
    public List<User> getFollowing(User user) {
        List<User> following;
        following = (List<User>) user.getFollowing();
        return following;
    }

    @Override
    public void addFollowing(User user, User following) {
        user.addFollower(following);
        em.merge(user);
    }
    @PostConstruct
    private void initUsers() {
        User u1 = new User("Hans", "http", "geboren 1");
        User u2 = new User("Frank", "httpF", "geboren 2");
        User u3 = new User("Tom", "httpT", "geboren 3");
        User u4 = new User("Sjaak", "httpS", "geboren 4");

        this.create(u1);
        this.create(u2);
        this.create(u3);
        this.create(u4);

        this.addFollowing(u1, u2);
        this.addFollower(u2, u1);
        this.addFollowing(u1, u3);
        this.addFollower(u3, u1);
        this.addFollowing(u1, u4);
        this.addFollower(u4, u1);

My own guess is that I’m missing a correct annotation in the User.java class, when looking at the Collection of User’s.

The error message:

Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLIntegrityConstraintViolationException: Column 'FOLLOWING_ID'  cannot accept a NULL value.
Error Code: -1
Call: INSERT INTO Users_Users (followers_ID, User_ID) VALUES (?, ?)
    bind => [2 parameters bound]
Query: DataModifyQuery(name="followers" sql="INSERT INTO Users_Users (followers_ID, User_ID) VALUES (?, ?)")
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:25:29+00:00Added an answer on June 17, 2026 at 7:25 pm

    Relationship between users and their followers is a bidirectional many-to-many relationship, not two one-to-many relationships, therefore you need to map it as such:

    @ManyToMany(mappedBy = "following")
    private Collection<User> followers = new ArrayList<>();
    
    @ManyToMany
    @JoinTable(name = "followers",
        joinColumn = @Column(name = "follower_id"),
        inverseJoinColumn = @Column(name = "following_id"))
    private Collection<User> following = new ArrayList<>();
    

    Also note that cascading usually should be used when referring to logically “owned” entities. In your case it makes no sense because Users don’t “own” each other.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.