Skip to content

No Hard Annotation for Schema Required

chhavigangwal edited this page Oct 18, 2013 · 15 revisions

Background :

Why support it ?

Earlier by hard coded schema in the entity a single entity 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 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 adhere to JPA compliance rules making hard annotation for schema in a class is made optional in Kundera

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;

For Polyglot persistence:

Define specific entity class mapping within tag explicitly in persistence.xml.

<persistence-unit name="noAnnotationAddMongo">
    <provider>com.impetus.kundera.KunderaPersistence</provider>
    <class>com.impetus.kundera.tests.entities.PersonDetail</class>
    ....
</persistence-unit>

**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;

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

Benefits :

  • Same class can be used for persisting data over multiple data stores in different schema using Kundera.
  • The entity available for a Nosql datastore can also be used with Hiberante thus preventing the changes required in existing apps using Hiberante.
Clone this wiki locally