Hibernate many-to-many join table not populating

Coxer

I want to associate two entity classes User and Unit via a many-to-many association using a join table. However this is not working as intended. First I'll briefly show you what I got so far.

Database:

|    User    |   |  UserToUnit   |   |   Unit    |
|Id|login|...|   |User_Id|Unit_Id|   |Id|name|...|

Entity User:

    @Entity
    @Table(name = "\"User\"")
    public class User implements Identifiable {
        public User() {
            units = new ArrayList<Unit>();
        }

        @Id
        @GeneratedValue(generator = "increment")
        @GenericGenerator(name = "increment", strategy = "guid")
        @Column(name = "Id", columnDefinition = "uniqueidentifier")
        private String id;

        @Transient
        @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
        @JoinTable(name = "UserToUnit",
            joinColumns = { @JoinColumn(name = "User_Id", nullable = false, updatable = false) },
            inverseJoinColumns =  { @JoinColumn(name = "Unit_Id", nullable = false, updatable = false) }
        )
        private Collection<Unit> units;
        [...]
        public Collection<Unit> getUnits() {
            return units;
        }

        public void setUnits(Collection<Unit> ous) {
            units= ous;
        }
    }

Entity Unit:

@Entity
@Table(name = "Unit")
@XmlRootElement
public class Unit implements Identifiable {

public Unit() {
    users = new ArrayList<User>();
}

@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "guid")
@Column(name = "Id", columnDefinition = "uniqueidentifier")
private String id;

@Transient
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "units")
private Collection<User> users;
[...]
public Collection<User> getUsers() {
    return users;
}

public void setUsers(Collection<User> us) {
    users = us;
}

Usage:

user.getUnits().add(unit);
unit.getUsers().add(user);
unitDAO.saveOrUpdate(unit);
userDAO.saveOrUpdate(user);

But this does not add anything to the UserToUnit table. What am I missing here? Cheers, Chris

Petros Splinakis

What is the reason why you are using the @Transient annotation? You should be using that if you want to use property access and the getter is not following the field name.

You are mixing field and property access, so you should also add @Access(AccessType.PROPERTY) to the getter method or move annotations to the field.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Doctrine many-to-many join table not populating

From Dev

Hibernate many-to-many mapping + join table update not working

From Dev

Hibernate Many-To-Many join table not persisting for inherited Entity

From Dev

Hibernate many-to-many association not updating join table

From Dev

Initializing many-to-many association in Hibernate with join table

From Dev

Hibernate one to many relationship with join table with addition columns in join table

From Dev

Many to Many relation with join table

From Dev

Hibernate join cannot cast exception many to many

From Dev

Hibernate join cannot cast exception many to many

From Dev

Hibernate, Use a constant in a many to many join

From Dev

Does a hibernate many to many relationship have to use the @ManyToMany annotation and use a join table?

From Dev

JPA/Hibernate: Map many-to-many relationship when join table has own primary key

From Dev

Hibernate deleting data from related join table for multiple Many-to-Many relationships

From Dev

How to insert constant value in a join table with Hibernate in a many-to-many relation?

From Dev

Hibernate: many-to-many relationship table as entity

From Dev

saveOrUpdate many-to-many table with Hibernate + MySQL

From Dev

Hibernate: many-to-many relationship table as entity

From Dev

Hibernate Many to Many without third table

From Dev

SQL join on junction table with many to many relation

From Dev

Many to many association without join table

From Dev

Sequelize not creating join table many-to-many

From Dev

Rails: many to many relationship join table design

From Dev

foreignKey or inverseForeignKey for many-to-many join table

From Dev

Is a "join table" the result of a SQL JOIN, or the table between a many-to-many

From Dev

Hibernate: which table is owner table in one-to-many unidirectional relationship using join table

From Dev

Hibernate Many-to-Many with join-class Cascading issue

From Dev

HIbernate many to one join criteria query

From Dev

HIbernate many to one join criteria query

From Dev

How to query join one to many in hibernate HQL

Related Related

  1. 1

    Doctrine many-to-many join table not populating

  2. 2

    Hibernate many-to-many mapping + join table update not working

  3. 3

    Hibernate Many-To-Many join table not persisting for inherited Entity

  4. 4

    Hibernate many-to-many association not updating join table

  5. 5

    Initializing many-to-many association in Hibernate with join table

  6. 6

    Hibernate one to many relationship with join table with addition columns in join table

  7. 7

    Many to Many relation with join table

  8. 8

    Hibernate join cannot cast exception many to many

  9. 9

    Hibernate join cannot cast exception many to many

  10. 10

    Hibernate, Use a constant in a many to many join

  11. 11

    Does a hibernate many to many relationship have to use the @ManyToMany annotation and use a join table?

  12. 12

    JPA/Hibernate: Map many-to-many relationship when join table has own primary key

  13. 13

    Hibernate deleting data from related join table for multiple Many-to-Many relationships

  14. 14

    How to insert constant value in a join table with Hibernate in a many-to-many relation?

  15. 15

    Hibernate: many-to-many relationship table as entity

  16. 16

    saveOrUpdate many-to-many table with Hibernate + MySQL

  17. 17

    Hibernate: many-to-many relationship table as entity

  18. 18

    Hibernate Many to Many without third table

  19. 19

    SQL join on junction table with many to many relation

  20. 20

    Many to many association without join table

  21. 21

    Sequelize not creating join table many-to-many

  22. 22

    Rails: many to many relationship join table design

  23. 23

    foreignKey or inverseForeignKey for many-to-many join table

  24. 24

    Is a "join table" the result of a SQL JOIN, or the table between a many-to-many

  25. 25

    Hibernate: which table is owner table in one-to-many unidirectional relationship using join table

  26. 26

    Hibernate Many-to-Many with join-class Cascading issue

  27. 27

    HIbernate many to one join criteria query

  28. 28

    HIbernate many to one join criteria query

  29. 29

    How to query join one to many in hibernate HQL

HotTag

Archive