No Hard Annotation for Schema Required

chhavigangwal edited this page Oct 18, 2013 · 15 revisions
Clone this wiki locally

Background :

Kundera has enabled flexibility to add entities without specifying any hardcoded schema annotation. For further details please refer github#315

Why support it ?

By giving hard coded schema definitions in an entity a single class could be used only for one datastore for a specific persistence unit.

A typical Entity class in Kundera :

@Entity
@Table(name = "KunderaUser", schema = "KunderaMetaDataTest@metaDataTest")
public class KunderaUser
{

    @Id
    @Column(name = "USER_ID")
    private String userId;

In the above model ,in case of polyglot persistence if an entity has to be used for different datastores it had to be replicated with details of its schema and persistence unit mentioned again. So in order to resolve this issue and also adhere to JPA specifications making hard annotation for schema in a class is now made optional in Kundera

However, Existing entities with hardcoded @Table annotation will continue to work as previously

Examples :

For Single persistence unit:

Optional @Table annotation :

Kundera will internally resolve schema and table name(Using kundera.keyspace property in persistence.xml), By default Entity name will be treated as table name unless it is explicitly provided.

@Entity
public class PersonDetail
{
    /** The person id. */
    @Id
    private String personId;

    /** The first name. */
    @Column(name = "first_name")
    private String firstName;

Entity with @Table annotation/without schema :

Kundera will internally resolve schema and table name(Using kundera.keyspace property in persistence.xml), The table name will be the one given with @Table Annotation.

@Entity
@Table(name = "person")
public class PersonDetail
{
    
    /** The person id. */
    @Id
    private String personId;

    /** The first name. */
    @Column(name = "first_name")
    private String firstName;

    /** The last name. */
    @Column(name = "last_name")
    private String lastName;

For Polyglot persistence:

Define entity class without any schema annotation in specific persistence-unit within class tag explicitly in persistence.xml and use same entity class to persist data in the multiple datastores.

<persistence-unit name="noAnnotationAddCassandra">
	 <provider>com.impetus.kundera.KunderaPersistence</provider>
	 <class>com.impetus.kundera.tests.entities.PersonDetail</class>
	 <exclude-unlisted-classes>true</exclude-unlisted-classes>
	 <properties>
	   <property name="kundera.nodes" value="localhost" />
	   <property name="kundera.port" value="9160" />
	   <property name="kundera.keyspace" value="KunderaTests" />
    ....
</persistence-unit>

<persistence-unit name="noAnnotationAddMongo">
	<provider>com.impetus.kundera.KunderaPersistence</provider>
	<class>com.impetus.kundera.tests.entities.PersonDetail</class>
	<exclude-unlisted-classes>true</exclude-unlisted-classes>
	<properties>
	   <property name="kundera.nodes" value="localhost" />
	   <property name="kundera.port" value="27017" />
	   <property name="kundera.keyspace" value="KunderaTests" />
	   <property name="kundera.dialect" value="mongodb" />
 ....
</persistence-unit>


Benefits :

  • Same class can be used for persisting data over multiple data stores in different schema.
  • The existing applications using Hiberante can also integrate a Nosql datastore using Kundera without re writing the entities for same data.
  • It will allow easy context switching of persistence units for same entity class.
  • Schema related parameters for an entity need not be replicated in every entity class.