Why do linked collections contain null values? (Hibernate, Annotation, Spring)

[Edit: Apparently, this is only a problem for arrays, and FoxyBOA's answer may direct (or even have) the answer.]

My question is about this software: Hibernate3 + Annotation, Spring MVC, MySQL, and in this example also Spring Security.

I was wondering why collections that are automatically linked to Hibernate contain null values ​​for each row number of the child table (in addition to the correct elements). My example:

I have a table of users and permissions, the primary key of the user table is the username that is used as a foreign key. Right now, 13 rows in the table of my authorities. When I retrieve the user from the database (MySQL InnoDB), and Hibernate automatically receives user privileges corresponding to this mapping:

@OneToMany
@JoinColumn(name = "username")
@IndexColumn(name="id") // "id" was the primary key and is used to sort the elements
public Authority[] getAuthorities() {
    return authorities;
}
public void setAuthorities(Authority[] authorities) {
    this.authorities = authorities;
}

... I get a collection of "permissions" containing 14 (0-13) elements, of which only four are non-zero (four rows in the database table belong to this particular user, so that’s right). As far as I understand, I use the default values ​​for Hibernate for properties like Fetchmode, etc. I get the user as follows:

Criteria criteria = getSession().createCriteria(User.class);
criteria.add(Restrictions.eq("username",username));
User user = (User) criteria.uniqueResult();

org.hibernate.loader.loader "" . , . :

java.lang.IllegalArgumentException: Granted authority element 0 is null - GrantedAuthority[] cannot contain any null elements
+3
2

. ( ), id . .

table authorities:
username id
bob 1
bob 3
bob 5

: {0 = null, 1 = bob, 2 = null, 3 = bob, 4 = null, 5 = bob}

UPDATE: :

  • (, 0,1,3,4,5 - 2. Hibernate 2 null).
  • , , (, HQL, ), u u.authorities a a.id = 2. , 3 : 0 - null, 1 - null, 2 - 2).
+2

@IndexColumn. id, "".

. hibernate

.

; ? , , .

+3

Source: https://habr.com/ru/post/1709092/


All Articles