I am trying to use japmodelgen in my springboot project to generate a static metamodel as per in Hibernate docs like below. Hibernate Docs
@OneToMany(mappedBy=Book_.PUBLISHER) // get used to doing it this way! Set<Book> books;
However, when I get maven to run build it always throws an error stating that it cannot find symbol for my USERACCOUNT. I'm not sure how to get it working and it seems to be the recommended way to ensure type safety by Hibernate docs as opposed to the stringly-type mapped-by reference.
I have tried renaming the variable in the related table but it does not work as well.For example, instead of USERACCOUNT I have tried userAccount as per the variable name in the UserAddress entity.
Here is my userAccount entity.
@Entity
public class UserAccount {
@Id
@CustomId(name ="userid_seq", prefix="user_") // userid sequence for custom unique id generation
private String userId;
@OneToMany(mappedBy=UserAddress_.USERACCOUNT) // One user can store many addresses
List<UserAddress> userAddresses;
package com.freshfeet.backend.model;
Below is my userAddress entity.
@Entity
public class UserAddress {
@ManyToOne(fetch=LAZY)
@JoinColumn(name="user_id", foreignKey = @ForeignKey(name="user_id")) //foreign key in UserAddressTable
private UserAccount userAccount;
@ManyToOne(fetch=LAZY)
@JoinColumn(name="address_id", foreignKey = @ForeignKey(name="address_id"))
private Address address;
Below you may find my pom.xml which has my build configuration
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.12.1</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>6.4.1.Final</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
Below you may find the error thrown by maven upon build.
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.12.1:compile (default-compile) on project backend: Compilation failure: Compilation failure:
[ERROR] /backend/src/main/java/com/freshfeet/backend/model/UserAccount.java:[18,37] cannot find symbol
[ERROR] symbol: variable USERACCOUNT
[ERROR] location: class com.freshfeet.backend.model.UserAddress_
This is what the generated class looks like.
package com.freshfeet.backend.model;
import jakarta.annotation.Generated;
import jakarta.persistence.metamodel.EntityType;
import jakarta.persistence.metamodel.SingularAttribute;
import jakarta.persistence.metamodel.StaticMetamodel;
@StaticMetamodel(UserAddress.class)
@Generated("org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
public abstract class UserAddress_ {
/**
* @see com.freshfeet.backend.model.UserAddress#getIsDefault
**/
public static volatile SingularAttribute<UserAddress, Boolean> isDefault;
/**
* @see com.freshfeet.backend.model.UserAddress
**/
public static volatile EntityType<UserAddress> class_;
public static final String IS_DEFAULT = "isDefault";
}
The problem was caused because my UserAddress table was missing a primary key. As I thought that it was a table breaking up a many to many relationship between UserAccounts and Addresses table there was no need for a primary key and just having two foreign keys in the table was sufficient. I solved the issue by creating an Id for UserAddress table in hibernate. I believe it is a conceptual gap on my part as ORMs require Ids on all tables inorder to build the relational tables.