Hibernate

From Null-pointer

Jump to: navigation, search

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

  1. <many-to-one class="Application" column="applicationId" name="applications" />

many to many

  1. <set name="groups" table="group_role">
  2.         <key column="role_id" />
  3.         <many-to-many class="Group" column="group_id" />
  4. </set>

one to many

  1. <set inverse="true" name="posts" order-by="postId asc">
  2.         <key>
  3.                 <column name="canvasId" not-null="true"/>
  4.         </key>
  5.         <one-to-many class="PostImpl"/>
  6. </set>

HQL

  1. <class catalog="canvas" name="com.linhopesolutions.canvas.model.community.Group" table="Group" >
  2.  <id name="groupId" type="integer" >
  3.   <generator class="identity" />
  4.  </id>
  5.  <property name="canopyUserId" type="integer" />
  6.  <property name="admin" type="boolean" />
  7.  <property name="defaultCommunity" type="boolean" />
  8.  <many-to-one class="com.linhopesolutions.canvas.model.community.Community"
  9.    column="communityId" name="Community"/>
  10. </class>

return only the communityId

  1. Query query = getQuery(" SELECT g.Community.communityId FROM "
  2.       + Group.class.getName()
  3.       + " AS g WHERE g.admin = true " );
  4.  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 ....

Personal tools