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

  • SEARCH
  • Home
  • 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 8752749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:17:46+00:00 2026-06-13T13:17:46+00:00

In my java spring webapp I have relationship: One account can have many groups

  • 0

In my java spring webapp I have relationship: One account can have many groups and one group can have only one account.

Group entity:

@Entity
@Table(name = "group")
@Inheritance(strategy = InheritanceType.JOINED)
public class Group {

@Id
@GeneratedValue(generator = "group_id", strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name = "group_id", sequenceName = "group_group_id_seq", allocationSize = 1)
@Column(name = "group_id")
private Long id;

@Basic
@NotBlank
@Column(name = "name")
private String name;

@ManyToOne(targetEntity = Account.class, fetch = FetchType.LAZY)
@JoinColumn(name = "account_id")
private Account account;

@Basic
@NotBlank
@Column(name = "all")
private boolean all;

@ManyToMany(targetEntity = Word.class, fetch = FetchType.LAZY)
@JoinTable(name = "group_word", joinColumns = {@JoinColumn(name="group_id")}, inverseJoinColumns = {@JoinColumn(name="word_id")})
private List<Word> words;

getters/setters...

Account entity:

@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
@Table(name = "account")
@Inheritance(strategy = InheritanceType.JOINED)
public class Account  extends User implements UserDetails {

@Id
@GeneratedValue(generator = "account_id", strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name = "account_id", sequenceName = "account_account_id_seq", allocationSize = 1)
@Column(name = "account_id")
private Long accountId;

@Column(name = "username")
private String username;

@Column(name = "password")
private String password;

@Transient
private String repeatedPassword;

@DateTimeFormat(iso = ISO.DATE_TIME)
@Column(name = "registration_date")
Date registrationDate = Calendar.getInstance().getTime();

@Column(name = "email")
private String email;

@ManyToMany(targetEntity = Role.class, fetch = FetchType.LAZY)
@JoinTable(name = "account_role", joinColumns = {@JoinColumn(name="account_id")}, inverseJoinColumns = {@JoinColumn(name="role_id")})
private List<Role> roles;

public List<Role> getRoles() {
    return roles;
}

public void setRoles(List<Role> roles) {
    this.roles = roles;
}

@OneToMany(mappedBy="account", fetch = FetchType.LAZY)
private List<Group> groups;

public List<Group> getGroups() {
    return groups;
}

public void setGroups(List<Group> groups) {
    this.groups = groups;

getters/setters...

When i invoke this service method:

public List<Group> listUserGroups(String username) {
    try {
        Account foundAccount = accountDao.findUserByUsername(username);
        List<Group> userGroups = foundAccount.getGroups();
        userGroups.size();
        return userGroups;
    } catch (UserNotFoundException unf) {
        logger.error("User not found: " + username, unf.getMessage());
        return Collections.emptyList();
    }
}

I have an error on line:

userGroups.size();

Error:

00:27:27,528 DEBUG [org.hibernate.engine.StatefulPersistenceContext] - initializing non-lazy collections
00:27:27,528 DEBUG [org.hibernate.loader.Loader] - loading collection: [pl.net.grodek.snd.model.Account.groups#116]
00:27:27,528 DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
00:27:27,528 DEBUG [org.hibernate.SQL] - select groups0_.account_account_id as account4_20_1_, groups0_.group_id as group1_1_, groups0_.group_id as group1_19_0_, groups0_.account_account_id as account4_19_0_, groups0_.all as all19_0_, groups0_.name as name19_0_ from group groups0_ where groups0_.account_account_id=?
00:27:27,544 DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
00:27:27,544 DEBUG [org.hibernate.util.JDBCExceptionReporter] - could not initialize a collection: [pl.net.grodek.snd.model.Account.groups#116] [select groups0_.account_account_id as account4_20_1_, groups0_.group_id as group1_1_, groups0_.group_id as group1_19_0_, groups0_.account_account_id as account4_19_0_, groups0_.all as all19_0_, groups0_.name as name19_0_ from group groups0_ where groups0_.account_account_id=?]
org.postgresql.util.PSQLException: ERROR: syntax error near "group"
Position: 227
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2103)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1836)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:512)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:273)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:76)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1953)
at org.hibernate.loader.Loader.doQuery(Loader.java:802)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
at org.hibernate.loader.Loader.loadCollection(Loader.java:2166)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:62)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:627)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:83)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1863)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:369)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111)
at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:134)
at org.hibernate.collection.PersistentBag.size(PersistentBag.java:248)
at pl.net.grodek.snd.service.GroupServiceImpl.listUserGroups(GroupServiceImpl.java:47)
at pl.net.grodek.snd.service.GroupServiceImpl.listUserGroups(GroupServiceImpl.java:1)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at 

I think that the problem is in query generated by jpa:

select groups0_.account_account_id as account4_20_1_, groups0_.group_id as group1_1_, groups0_.group_id as group1_19_0_, groups0_.account_account_id as account4_19_0_, groups0_.all as all19_0_, groups0_.name as name19_0_ from group groups0_ where groups0_.account_account_id=?

where there are doubled account_id and group_id. But my question why this is happening. Please clarify for me this issue because I ran out of ideas 🙁

EDIT:

Oh i forgot about DB tables:

CREATE TABLE "group"
(
    group_id bigserial NOT NULL,
    name character varying(150)[] NOT NULL,
    "all" boolean NOT NULL DEFAULT false,
    account_id bigint NOT NULL,
    CONSTRAINT group_id_pk PRIMARY KEY (group_id ),
    CONSTRAINT account_id_fk FOREIGN KEY (account_id)
    REFERENCES account (account_id) MATCH SIMPLE
    ON UPDATE NO ACTION ON DELETE NO ACTION
)

CREATE TABLE account
(
    account_id bigserial NOT NULL,
    username character varying(32),
    password character varying(64),
    email character varying(100),
    registration_date timestamp with time zone NOT NULL,
    word_for_today bigint,
    groups_on_page integer NOT NULL,
    CONSTRAINT account_pk PRIMARY KEY (account_id ),
    CONSTRAINT word_fk FOREIGN KEY (word_for_today)
  REFERENCES word (word_id) MATCH SIMPLE
  ON UPDATE NO ACTION ON DELETE NO ACTION,
    CONSTRAINT unique_mail UNIQUE (email ),
    CONSTRAINT unique_username UNIQUE (username )
)
  • 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-13T13:17:47+00:00Added an answer on June 13, 2026 at 1:17 pm

    I changed group table name to groups and it works. but please can anyone tell me why is that. With earlier relationships i did not have such mysterious problems. I am loosing ma sanity. 🙁

    Maybe someone sees a problem in my code? Are my account->group relationships well set?

    EDIT: As @AdrianShum said group is a SQL keyword so that my mapping worked when I changed to another name

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

Sidebar

Related Questions

I am writing java/spring webapp in which every user can have its own word
(Java Spring WebApp) I have: public class PersonValidator implements Validator { private PersonDAO d;
I have a Java/Spring MVC/JSP/JSTL webapp. We have a designer that gave us some
I am using java spring framework to develop a Webapp. I have a situation
I am using Spring MVC to develop a Java webapp. I have a setup
I have a java/spring webapp which uses HibernateTemplate and I was wondering if it
Answer: My Spring 3 Java webapp is acme. I had this for my JQuery
I've been converting a Java Servlet based webapp as a means of learning Spring.
In my Java spring application I have a form with Region drop down. On
I have a web application, built with Maven, consisting of a Java (Spring) backend

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.