Hibernate
From Null-pointer
Contents |
hibernate.cfg.xml
<!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">create</property>
hibernate.hbm2ddl.auto
Automatically validates or exports schema DDL to the database when the SessionFactory is created. With create-drop, the database schema will be dropped when the SessionFactory is closed explicitly.
- validate
- update
- create
- create-drop
Mapping.hbm.xml
many to one
- <many-to-one class="Application" column="applicationId" name="applications" />
many to many
- <set name="groups" table="group_role">
- <key column="role_id" />
- <many-to-many class="Group" column="group_id" />
- </set>
one to many
- <set inverse="true" name="posts" order-by="postId asc">
- <key>
- <column name="canvasId" not-null="true"/>
- </key>
- <one-to-many class="PostImpl"/>
- </set>
HQL
- <class catalog="canvas" name="com.linhopesolutions.canvas.model.community.Group" table="Group" >
- <id name="groupId" type="integer" >
- <generator class="identity" />
- </id>
- <property name="canopyUserId" type="integer" />
- <property name="admin" type="boolean" />
- <property name="defaultCommunity" type="boolean" />
- <many-to-one class="com.linhopesolutions.canvas.model.community.Community"
- column="communityId" name="Community"/>
- </class>
return only the communityId
- Query query = getQuery(" SELECT g.Community.communityId FROM "
- + Group.class.getName()
- + " AS g WHERE g.admin = true " );
- query.list();
Annotations
extending
@Entity public class Application extends Base { ... }
@MappedSuperclass public abstract Base { ... }
SequenceStyleGenerator
Using org.hibernate.id.enhanced.SequenceStyleGenerator
... @Id @GeneratedValue(generator = "SEQ_STORE") @GenericGenerator(name = "SEQ_STORE", strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator", parameters = { @Parameter(name = "sequence_name", value = "sequence"), @Parameter(name = "increment_size", value = "1000"), @Parameter(name = "optimizer", value = "hilo") }) @Column(name = "id") public int getId() { return id; } ...
HibernateSystemException: Unknown entity
check your imports
correct
import javax.persistence.Entity; @Entity public class Application ....
wrong
import org.hibernate.annotations.Entity; @Entity public class Application ....

