Not an '@Entity' type error?

Hi! I could use a little help getting started with Hibernate ORM, mapping, and associations. In particular, I’m getting this runtime error which is a little puzzling:

org.hibernate.AnnotationException: Association 'model.Account.orders' targets the type 'model.Order' which is not an '@Entity' type

This is puzzling to me because my model.Order Java POJO does have the @Entity annotation. Being POJOs, there’s not much to them, but FWIW here they are. I’m sure I’m overlooking something silly, but I don’t quite see it yet. Any insights would be greatly appreciated. Thanks!

package model;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;

@Entity
public class Account {
    @Id @GeneratedValue
    public UUID id;
    public String name;
    @Column(name = "created_at") @CreationTimestamp
    public LocalDateTime createdAt;
    @Column(name = "updated_at") @UpdateTimestamp
    public LocalDateTime updatedAt;
    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    public List<Order> orders = new ArrayList<>();
}
package model;

import java.time.LocalDateTime;
import java.util.UUID;

import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;

@Entity
public class Order {
    @Id @GeneratedValue
    public UUID id;
    public UUID accountId;
    public String status;
    public String value;
    @Column(name = "created_at") @CreationTimestamp
    public LocalDateTime createdAt;
    @Column(name = "updated_at") @UpdateTimestamp
    public LocalDateTime updatedAt;
    @ManyToOne @JoinColumn(name = "account_id")
    public Account account;
}

You probably use an explicit class list somewhere (e.g. persistence.xml) and didn’t list Order there.

1 Like

Thank you! That must be it. I don’t have a persistence.xml file, but I do explicitly add the Account model class to the Configuration in the test code, but neglected to add Order:

    @BeforeEach
    void setUp() {
	Configuration cfg = new Configuration()
	    .setProperty("hibernate.show_sql", "True")
	    .setProperty("hibernate.format_sql", "True")
	    .setProperty("hibernate.highlight_sql", "True")
	    .setProperty("hibernate.connection.driver_class", "org.postgresql.Driver")
	    .setProperty("hibernate.connection.url", postgres.getJdbcUrl())
	    .setProperty("hibernate.connection.username", postgres.getUsername())
	    .setProperty("hibernate.connection.password", postgres.getPassword())
	    .addAnnotatedClass(Account.class);

	final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
	    .build();
	try {
	    sessionFactory = cfg.buildSessionFactory();
	}
	catch (Exception e) {
	    e.printStackTrace(System.err);
	    StandardServiceRegistryBuilder.destroy(registry);
	}
    }

Is there a way to avoid an explicit class list anywhere? If I have a lot of entity classes, I’d like not to have to list them either in persistence.xml, in Java code, in XML files, or anywhere else, and instead have them just “picked up.” Maybe that’s not possible though. Anyway, thanks much.

Refer to the documentation regarding the details about archive scanning. You can set the hibernate.archive.autodetection property to class and Hibernate ORM should pickup classes automatically.

1 Like