Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Hibernate and User Defined Types Persistence

  Asked By: Phil    Date: Nov 03    Category: Java    Views: 1208
  

How can I persist a class with user defined-types attributes using
Hibernate?

For instance: I´d like Hibernate to store the result of getCNPJ().toString()
method into the CompanyTable at DBMS. However, it seems Hibernate serializes
the CNPJ object and stores it at DBMS.


----------------------------------------------------
package my.package.entity;

import my.package.types.Type_CNPJ;

public class CompanyEntity {
private Integer id;
public Integer getId() { return this.id; }
public setId( Integer newValue ) { this.id = newValue; }

private Type_CNPJ cnpj;
public Type_CNPJ getCNPJ() { return this.cnpj; }
public setCNPJ( Type_CNPJ newValue ) { this.CNPJ = newValue; }
}
----------------------------------------------------
CREATE TABLE [CompanyTable] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[CNPJ] [varchar] (14) NOT NULL
) ON [PRIMARY]
----------------------------------------------------
<hibernate-mapping>
<class name="my.package.entity.CompanyEntity " table="CompanyTable">
<id name="id" type="integer" column="ID">
<generator class="native"/>
</id>
<property name="cnpj" column="CNPJ" type="my.package.types.Type_CNPJ" />
</class>
</hibernate-mapping>

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Sheryl Morgan     Answered On: Nov 03


You can create  another propety in the class  cNPJString
where getCNPJString() returns CNPJ.toString() and give
the mapping of cNPJString property instead of CNPJ.

 
Didn't find what you were looking for? Find more on Hibernate and User Defined Types Persistence Or get search suggestion and latest updates.




Tagged: