“Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements”

The Exception is straightforward and says : Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements, so the cause is obvious here and if we take a look at the Hibernate Collection mapping documentation it clearly states that: As a requirement persistent collection-valued fields must be declared as an interface type … Read more

Hibernate mapping between PostgreSQL enum and Java enum

You can simply get these types via Maven Central using the Hypersistence Util dependency: <dependency> <groupId>io.hypersistence</groupId> <artifactId>hypersistence-utils-hibernate-55</artifactId> <version>${hibernate-types.version}</version> </dependency> If you easily map Java Enum to a PostgreSQL Enum column type using the following custom Type: public class PostgreSQLEnumType extends org.hibernate.type.EnumType { public void nullSafeSet( PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, … Read more

How can I map “insert=’false’ update=’false'” on a composite-id key-property which is also used in a one-to-many FK?

I think the annotation you are looking for is: public class CompanyName implements Serializable { //… @JoinColumn(name = “COMPANY_ID”, referencedColumnName = “COMPANY_ID”, insertable = false, updatable = false) private Company company; And you should be able to use similar mappings in a hbm.xml as shown here (in 23.4.2): http://docs.jboss.org/hibernate/core/3.3/reference/en/html/example-mappings.html

How can I mark a foreign key constraint using Hibernate annotations?

@Column is not the appropriate annotation. You don’t want to store a whole User or Question in a column. You want to create an association between the entities. Start by renaming Questions to Question, since an instance represents a single question, and not several ones. Then create the association: @Entity @Table(name = “UserAnswer”) public class … Read more

Hibernate table not mapped error in HQL query

The exception message says: Books is not mapped [SELECT COUNT(*) FROM Books]; nested exception is org.hibernate.hql.ast.QuerySyntaxException: Books is not mapped [SELECT COUNT(*) FROM Books] Books is not mapped. That is, that there is no mapped type called Books. And indeed, there isn’t. Your mapped type is called Book. It’s mapped to a table called Books, … Read more

How to map calculated properties with JPA and Hibernate

JPA doesn’t offer any support for derived property so you’ll have to use a provider specific extension. As you mentioned, @Formula is perfect for this when using Hibernate. You can use an SQL fragment: @Formula(“PRICE*1.155”) private float finalPrice; Or even complex queries on other tables: @Formula(“(select min(o.creation_date) from Orders o where o.customer_id = id)”) private … Read more

In which case do you use the JPA @JoinTable annotation?

EDIT 2017-04-29: As pointed to by some of the commenters, the JoinTable example does not need the mappedBy annotation attribute. In fact, recent versions of Hibernate refuse to start up by printing the following error: org.hibernate.AnnotationException: Associations marked as mappedBy must not define database mappings like @JoinTable or @JoinColumn Let’s pretend that you have an … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)