I'm STILL dealing with a non-Spring legacy application, with a matching legacy database that is not easily updateable, so I have to deal with the cards that were dealt me.
I'm moving this non-Spring app from Hibernate 3 to Hibernate 5 and I was running into some issues so let me say they have a primary-hibernate.cfg.xml with these three classes, in this order:
<mapping class="com.app.server.model.user.UserRolePK" />
<mapping class="com.app.server.model.user.UserRole" />
<mapping resource="com/app/server/model/user/User.hbm.xml"/>
The first classes is defined as follows:
public class UserRolePK implements Serializable {
@Column(insertable=false, updatable=false)
private Long userId;
@Column(name = "elt", insertable=false, updatable=false)
@Enumerated(EnumType.STRING)
private Role role;
// getters/setters/equals/hashcode/toString
}
The main class looks like this:
@Entity
@Table(name="roles")
@IdClass(UserRolePK.class)
public class UserRole implements Serializable {
@Id
@ManyToOne(fetch = FetchType.LAZY, optional=false)
@JoinColumn(name = "userId", referencedColumnName = "id", insertable=false, updatable=false)
private User user;
@Id
@Column(name = "elt", insertable=false, updatable=false)
@Enumerated(EnumType.STRING)
private Role role;
// getters/setters/equals/hashcode/toString
}
I can tell you I created a Dao Class for the UserRole with UserRolePK and they work 100%. I can access the database and get all the records from the table 'roles' with no problem. So, I know this part works.
Finally the User.hbm.xml looks like this:
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.app.server.model.user">
<class name="User" table="user">
<id name="id" column="id">
<generator class="native" />
</id>
<bag name="roles" cascade = "all,delete-orphan" lazy="false">
<key column="userId" not-null="true"/>
<one-to-many class="com.app.server.model.user.UserRole"/>
</bag>
// more properties not relevant at this time
</class>
</hibernate-mapping>
I thought this was all good, but I am getting an error when I run the application. I get this nasty error:
org.hibernate.boot.MappingException: Association [com.app.server.model.user.User.roles] references an unmapped entity [com.app.server.model.user.User.roles] : origin(com/app/server/model/user/User.hbm.xml)
at org.hibernate.boot.model.source.internal.hbm.ModelBinder$AbstractPluralAttributeSecondPass.bindCollectionTable(ModelBinder.java:3195)
at org.hibernate.boot.model.source.internal.hbm.ModelBinder$AbstractPluralAttributeSecondPass.doSecondPass(ModelBinder.java:3133) ...
more error messages
Now I am experimenting with the 'roles' in a variety of ways, add a table:
<bag name="roles" table="roles" cascade = "all,delete-orphan" lazy="false">
<key column="userId" not-null="true"/>
<one-to-many class="com.app.server.model.user.UserRole"/>
</bag>
Or, change from bag to set:
<set name="roles" table="roles" cascade = "all,delete-orphan" lazy="false">
<key column="userId" not-null="true"/>
<one-to-many class="com.app.server.model.user.UserRole"/>
</set>
But this doesn't work. I would love to turn all the hbm.xml files into annotated tables, but the legacy app I am working on already has a class defined: com.app.server.model.user.User and unfortunately, they decided to stick a bunch of business logic into a hibernate entity. This business logic changes the values of things, and I wouldn't do that here, so I may have to change it to someplace else.
Hopefully, this is an easy fix within the User.hbm.xml file and it will make my error go away.