Converter doesn't work and not taken by criteria - converters

i'm new here^^
i'm working with a db in which boolean are registered as VARCHAR(3) So i made a converter :
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
#Converter
public class BooleanToStringConverter implements
AttributeConverter<Boolean, String> {
#Override
public String convertToDatabaseColumn(Boolean value) {
if (value == null)
return "";
else if (value)
return "OUI";
else
return "NON";
}
#Override
public Boolean convertToEntityAttribute(String value) {
if (value == null)
return null;
else if (value.trim().equals("OUI"))
return true;
else if (value.trim().equals("NON"))
return false;
else if (value.trim().equals(""))
return null;
else
throw new IllegalStateException("Invalid boolean character: "
+ value);
}
}
i put my annotations #Convert:
public class Institution extends AbstractHREntity {
#Column(name = "IDOU00")
private String code;
#Column(table = "ZEXX", name = "LBOULG")
private String libelle;
#Column(table = "ZEXX", name = "LBOUSH")
private String abreviation;
#Column(table = "ZEYY", name = "LIBEXL")
private String libelleLong;
#Convert(converter = BooleanToStringConverter.class)
#Column(table = "ZEZZ", name = "ESTBUDGET", length = 3)
private Boolean isBudget;[/CODE]
But when i do a request with a criteria :
public List<Institution> findInstitutions(RechercheInstitutionData datas) throws DaoException{
List<Institution> resultList = new ArrayList<Institution>();
DetachedCriteria criteria = DetachedCriteria.forClass(Institution.class, "institution");
if(null!=datas.getInstitutionSearched())
{
if (StringUtils.isNotBlank(datas.getInstitutionSearched().getLibelleLong())){
criteria.add(Restrictions.like("institution.libelleLong", datas.getInstitutionSearched().getLibelleLong().toUpperCase(), MatchMode.START));
}
if (StringUtils.isNotBlank(datas.getInstitutionSearched().getAbreviation())){
criteria.add(Restrictions.like("institution.abreviation", datas.getInstitutionSearched().getAbreviation().toUpperCase(), MatchMode.START));
}
if (StringUtils.isNotBlank(datas.getInstitutionSearched().getLibelle())){
criteria.add(Restrictions.like("institution.libelle", datas.getInstitutionSearched().getLibelle(), MatchMode.START).ignoreCase());
}
if (StringUtils.isNotBlank(datas.getInstitutionSearched().getCode())){
criteria.add(Restrictions.like("institution.code", datas.getInstitutionSearched().getCode(), MatchMode.START));
}
criteria.addOrder(Order.asc("institution.code"));
}
resultList = find(criteria);
return resultList;
}
i had this error that occurred :
10:25:31,172 INFO [RechercheInstitution.beans.RechercheInstitutionBean] (http-localhost/127.0.0.1:8080-5) --> findInstitutions()
10:25:32,549 INFO [stdout] (http-localhost/127.0.0.1:8080-5) Hibernate: select this_.NUDOSS as NUDOSS1_35_0_, this_.IDOU00 as IDOU2_35_0_, this_1_.TBUDGE as TBUDGE1_39_0_, this_2_.LIBEXL as LIBEXL1_37_0_, this_3_.LBOUSH as LBOUSH1_36_0_, this_3_.LBOULG as LBOULG2_36_0_ from ZE00 this_ left outer join ZEWD this_1_ on this_.NUDOSS=this_1_.NUDOSS left outer join ZE04 this_2_ on this_.NUDOSS=this_2_.NUDOSS left outer join ZE01 this_3_ on this_.NUDOSS=this_3_.NUDOSS where this_3_.LBOUSH like ? order by this_.IDOU00 asc
10:25:33,310 WARN [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost/127.0.0.1:8080-5) SQL Error: -99999, SQLState: 07006
10:25:33,311 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost/127.0.0.1:8080-5) Data type mismatch. (For input string: "OUI")
10:25:33,313 ERROR [RechercheInstitution.ejb.institution.InstitutionMgrBean] (http-localhost/127.0.0.1:8080-5) InstitutionMgrBean.findInstitutions : common_HR.exception.DaoException: org.hibernate.exception.SQLGrammarException: could not execute query
10:25:33,315 INFO [RechercheInstitution.beans.RechercheInstitutionBean] (http-localhost/127.0.0.1:8080-5) <-- findInstitutions()
It looks like it doesn't use my converter, i tried breakpoints it doesn't go into my converter... In converter doc it says "all you have to do is put the #convert" but apparently no... Please help i'm really lost.

Related

Genson not using my custom Converter for inner structure in JerseyTest

I have a class hierarchy that is somewhat like below, with a custom Converter
The FieldValueConverter#deserialize is NOT being called in my JerseyTest. Instead, it uses the default GensonJsonConverter which complains it can't find the appropriate constructor. (Caused by: com.owlike.genson.JsonBindingException: No constructor has been found for type class com.searchdata.actions.api.FieldValue)
How do I get it to be used?
Registration
The converter for the FieldValues (see below), which I register in a Jersey Application like this:
Genson genson = new GensonBuilder()
.withBundle(new JAXBBundle())
.withConverter(new FieldValueConverter(), FieldValue.class)
.setSkipNull(true)
.create();
register(new GensonJaxRSFeature().use(genson));
FieldValueConverter
public class FieldValueConverter implements Converter<FieldValue> {
private static final Logger LOG = LoggerFactory.getLogger(FieldValueConverter.class);
public void serialize(FieldValue fieldValue, ObjectWriter writer, Context ctx) throws Exception {
LOG.info("Serializing fieldValue:{}", fieldValue);
writer.beginObject();
writer.writeString("type", fieldValue.getType().name())
.writeString("value", fieldValue.getValue().toString())
.writeString("field", fieldValue.getField());
writer.endObject();
LOG.info("..Done!", fieldValue);
}
/* You don't have to worry for the object being null here, if it is null Genson will
handle it for you. */
public FieldValue deserialize(ObjectReader reader, Context ctx) throws Exception {
LOG.info("Deserializing fieldValue...");
reader.beginObject();
String stringValue=null;
FieldType type= FieldType.STRING;
String fieldKey= null;
while (reader.hasNext()) {
reader.next();
if ("type".equals(reader.name())) {
type = FieldType.valueOf(reader.valueAsString());
} else if ("field".equals(reader.name())) {
fieldKey = reader.valueAsString();
} else if ("value".equals(reader.name())) {
stringValue = reader.valueAsString();
} else {
reader.skipValue();
}
}
Item
public class Item
{
#Schema(name = "id", description = "The id of an item")
private String id;
#Schema(name = "values", description = "The fields with values for this action")
private List<FieldValue> values;
}
FieldValue
#Schema(name = "FieldValue")
#JsonInclude(JsonInclude.Include.NON_DEFAULT)
public class FieldValue {
#Schema(name = "field", description = "The technical name of the field")
private String field;
#Schema(name = "type", description = "The type of the field")
private FieldType type;
#Schema(name = "value", description = "The value of a field", oneOf = {Integer.class, String.class, Date.class, Double.class})
private Serializable value;
public FieldValue(final String field, final String string) {
setField(field);
setValue(string);
setType(FieldType.STRING);
}
public FieldValue(final String field, final Long number) {
setField(field);
setValue(number);
setType(FieldType.LONG);
}

How do I insert values of elements that are part of the EmbeddedId in JPA?

I have a case where I need to execute an insert statement via createNativeQuery. I have an entity list I'm looping through in order to set the properties accordingly from another bean class, and then persist that data to the oracle database.
The problem I am facing is persisting the data that is part of the embeddedId (item, loc, weekstart, type, forecastId, insertTS). I need to persist that data for the new records to be inserted into the database. When I try to set the values from the POJO bean to my set method for the properties of my entity bean, nothing happens. Below is my code for setting the values of the properties from the POJO bean to my entity bean, along with my persistence method and the insert query being executed:
Validation class where validation occurs beforehand (missing to get the point) that includes the setting of my entity properties from the POJO bean:
List <InsertPromoData> insertPromos = new ArrayList<InsertPromoData>();
promo.forEach(record -> {
if (record.getErrorList().size() == 0) {
rowsSuccessful++;
Util.writeSuccessToFile(templateCd, successFile, record, successFields);
try {
InsertPromoData insertData = new InsertPromoData();
insertData.getId().setItem(record.getItem());
insertData.getId().setLoc(record.getLoc());
insertData.getId().setWeekStart(record.getWeek_Start_Date());
insertData.setNumberOfWeeks(record.getNumber_Of_Weeks());
insertData.getId().setType(record.getType());
insertData.getId().setForecastId(record.getForecast_ID());
insertData.setQty(record.getUnits());
insertPromos.add(insertData);
}
catch (Exception e) {
logger.error("Error with setting insertPromolist from promo list values and the error is " + e.getMessage());
}
}
else {
if (rowsFailure == 0) {
Util.writeHeaderToFile(templateCd, errorFile);
}
rowsFailure++;
Util.writeErrorToFile(templateCd, errorFile, record, record.getErrorList());
}
});
errorFile.close();
successFile.close();
OracleImpl.insertPromoData(insertPromos);
POJO bean (promo is the variable representing this list of beans in validation class above):
public class PromoBean extends ErrorListBean
{
public String Item;
public String Loc;
public String Week_Start_Date;
public String Units;
public String Forecast_ID;
public String Type;
public String Number_Of_Weeks;
public String getItem() {
return Item;
}
public void setItem(String item) {
Item = item;
}
public String getLoc() {
return Loc;
}
public void setLoc(String loc) {
Loc = loc;
}
public String getWeek_Start_Date() {
return Week_Start_Date;
}
public void setWeek_Start_Date(String week_Start_Date) {
Week_Start_Date = week_Start_Date;
}
public String getNumber_Of_Weeks() {
return Number_Of_Weeks;
}
public void setNumber_Of_Weeks(String number_Of_Weeks) {
Number_Of_Weeks = number_Of_Weeks;
}
public String getType() {
return Type;
}
public void setType(String type) {
Type = type;
}
public String getForecast_ID() {
return Forecast_ID;
}
public void setForecast_ID(String forecast_ID) {
Forecast_ID = forecast_ID;
}
public String getUnits() {
return Units;
}
public void setUnits(String units) {
Units = units;
}
}
Embeddable class representing the composite primary key of the table:
#Embeddable
public class PromoID implements Serializable {
#Column(name = "ITEM")
private String item;
#Column(name = "LOC")
private String loc;
#Column(name = "WK_START")
private String weekStart;
#Column(name = "TYPE")
private String type;
#Column(name = "FCSTID")
private String forecastId;
#Column(name = "U_TIMESTAMP")
private String insertTS;
public PromoID() {
}
public PromoID (String item, String loc, String weekStart, String type, String forecastId, String insertTS) {
this.item = item;
this.loc = loc;
this.weekStart = weekStart;
this.type = type;
this.forecastId = forecastId;
this.insertTS = insertTS;
}
public String getItem() {
return item;
}
public void setItem(String item) {
this.item = item;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
public String getWeekStart() {
return weekStart;
}
public void setWeekStart(String weekStart) {
this.weekStart = weekStart;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getForecastId() {
return forecastId;
}
public void setForecastId(String forecastId) {
this.forecastId = forecastId;
}
public String getInsertTS() {
return insertTS;
}
public void setInsertTS(String insertTS) {
this.insertTS = insertTS;
}
//hashcode and equals methods
Persistence Bean:
#Entity
#Table(name = "U_USER_PROMO")
public class InsertPromoData {
#EmbeddedId
private PromoID id;
#Column(name="NUMBER_OF_WEEKS")
String numberOfWeeks;
#Column(name="QTY")
String qty;
#Id
#AttributeOverrides(
{
#AttributeOverride(name = "item",column = #Column(name="ITEM")),
#AttributeOverride(name = "loc", column = #Column(name="LOC")),
#AttributeOverride(name = "weekStart", column = #Column(name="WK_START")),
#AttributeOverride(name = "type", column = #Column(name="TYPE")),
#AttributeOverride(name = "forecastId", column = #Column(name="FCSTID"))
}
)
public PromoID getId() {
return id;
}
public void setId(PromoID id) {
this.id = id;
}
public String getNumberOfWeeks() {
return numberOfWeeks;
}
public void setNumberOfWeeks(String numberOfWeeks) {
this.numberOfWeeks = numberOfWeeks;
}
public String getQty() {
return qty;
}
public void setQty(String qty) {
this.qty = qty;
}
}
DAO class method to execute the update (entitymanagerfactory emf already initialized):
public static void insertPromoData(List<InsertPromoData> insertData) {
logger.debug("Execution of method insertPromoData in Dao started");
System.out.println("Size of the insertData list is " + insertData.size());
EntityManager em = emf.createEntityManager();
try {
em.getTransaction().begin();
System.out.println("Beginning transaction for insertPromoData");
Query query = em.createNativeQuery(env.getProperty("insertPromoUploadData"));
for (InsertPromoData promoData : insertData) {
query.setParameter("item", promoData.getId().getItem());
query.setParameter("location", promoData.getId().getLoc());
query.setParameter("wkStart", promoData.getId().getWeekStart());
query.setParameter("numberOfWeeks", promoData.getNumberOfWeeks());
query.setParameter("type", promoData.getId().getType());
query.setParameter("fcstId", promoData.getId().getForecastId());
query.setParameter("quantity", promoData.getQty());
query.executeUpdate();
}
em.getTransaction().commit();
}
catch(Exception e) {
logger.error("Exception in beginning transaction");
e.printStackTrace();
}
finally {
em.clear();
em.close();
}
logger.debug("Execution of method insertPromoData in Dao ended");
}
Query in properties file:
insertPromoUploadData = INSERT INTO {h-schema}U_USER_PROMO (ITEM, LOC, WK_START, NUMBER_OF_WEEKS, TYPE, FCSTID, QTY, U_TIMESTAMP) VALUES (:item, :location, TO_DATE(:wkStart,'MM DD YYYY'), :numberOfWeeks, :type, :fcstId, :quantity, SYSDATE)
My list size from my DAO class is returning as 0 once I begin the transaction and not sure why it is empty. Is there a reason that it is empty? I'm trying to persist each of the fields to the database (including the composite key fields) via insert query. Any help appreciated.
After looking into this for hours, I finally came to the conclusion that the simplest way to executeUpdate() without running into issues due to my current #EmbeddedId/#Embeddable logic was to change it to use #IdClass for my composite PK class, and annotate the fields from the PK in my entity with #Id. This allowed my data to be persisted to the database. Another slight difference was adding the insertTS in my entity class and annotating with #Id and generating getters/setters. This was necessary for JPA to recognize all the properties being referenced that I am wanting to persist, though I am persisting insertTS using SYSDATE function from the oracle DB instead of utilizing the get/set methods and setting to the current time from the java side.
I am sure there is a way to use #EmbeddedId/#Embeddable logic and be able to persist the fields that are part of the EmbeddedId, however, this I found to be a more simplistic way of doing it without further complexity in the code.

fetch all the data from main table and all the corresponding child record with activeYn != N., using spring jpa

Trying to achieve
fetch all the data from main table and all the corresponding child record with activeYn != N.
This is parent entity
#Entity
#Table(name="IR_TB_INCIDENT_HDR")
public class IncidentHdr implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#SequenceGenerator(name="IR_TB_INCIDENT_HDR_INCIDENTID_GENERATOR", sequenceName="IR_SEQ_INCIDENT_ID",allocationSize=1)
#GeneratedValue(strategy=GenerationType.SEQUENCE, generator="IR_TB_INCIDENT_HDR_INCIDENTID_GENERATOR")
#Column(name="INCIDENT_ID")
private long incidentId;
#Column(name="PATIENT_ID")
private Long patientId;
#OneToMany(cascade = {CascadeType.PERSIST,CascadeType.MERGE, CascadeType.REMOVE}, fetch = FetchType.LAZY, mappedBy="incidentHdr")
private Set<Attachments> attachments;
....
//other child entities
}
This is the child entity
#Entity
#Table(name="IR_TB_ATTACHMENTS")
public class Attachments implements Serializable {
private Long attachmentId;
private IncidentHdr incidentHdr;
private boolean activeYn;
}
Here we are genegating the custom query, we are appending only one condition here.
public IncidentHdr findIncidentDetailForId(Long incidentId) throws BusinessException {
StringBuilder query = null;
IncidentHdr incidentHdr = null;
StringBuilder incidentDetailQuery = null;
Query q = null;
Map < String, Object > parameters = new HashMap < String, Object > ();
List < String > criteria = new ArrayList < String > ();
try {
incidentDetailQuery = new StringBuilder();
query = new StringBuilder();
query.append(ReposJPQL.GET_INCIDENTS_DETAIL);
criteria.add("inc.incidentId = :incidentId ");
parameters.put("incidentId", incidentId);
if (criteria.size() > 0) {
for (int i = 0; i < criteria.size(); i++) {
incidentDetailQuery.append(" AND ");
incidentDetailQuery.append(criteria.get(i));
}
}
query.append(incidentDetailQuery);
q = em.createQuery(query.toString());
for (Entry < String, Object > entry: parameters.entrySet()) {
q.setParameter(entry.getKey(), entry.getValue());
}
incidentHdr = (IncidentHdr) q.getSingleResult();
}catch(IllegalArgumentException | IllegalStateException | DataAccessException | EntityNotFoundException e) {
logger.error(e.getMessage());
throw new BusinessException(e);
}
return incidentHdr;
}
ReposJPQL, Here defined the query with activeYn condition.
public interface ReposJPQL {
public String GET_INCIDENTS_DETAIL = "SELECT inc "
+ " FROM IncidentHdr inc left join inc.attachments att WHERE att.activeYn <> 'N' ";
}
Even though the records are present it return "org.springframework.dao.EmptyResultDataAccessException: No entity found for query; nested exception is javax.persistence.NoResultException: No entity found for query"
error
Or is there any other way to achieve this ? #Where(clause=...) option is pure hibernate so cant use that.

Hibernate not retrieving correct value with SELECT and ORA-00984: column not allowed here error

I am facing two issues here :
Issue 1) I am trying to get the COMPANY_ID using the selectCompanyID variable as shown below. I am storing the value returned into getCompanyID variable.When I tried to print it, as shown in the console output below, it's always printing number 1. However, when I tried to run the actual SQL in the
Oracle SQL developer SQL Worksheeet, I got different number. Because of this I could insert wrong ID in the following INSERT statement where it's
getting used.
Issue 2) As shown in the SQL, I am also getting ORA-00984: column not allowed here error. Noticed, people encountering similar error in the following post :
"column not allowed here" error in INSERT statement
But here I don't have straightforward SQL INSERT statement as the one mentioned in the above post with missing quotes.
public boolean insertEmployeeDetails(Employee employee)
{
logger.debug("Starting EmployeeDaoImpl.insert() .....");
Session session = null;
Transaction tx = null;
boolean status = true;
try {
session = sessionFactory.openSession();
tx = session.beginTransaction();
//The following SELECT query returns a number when I ran it in the Oracle SQL developer SQL worksheet
String selectCompanyID = "SELECT"
+ " VALUE_EMP_ID"
+" FROM "
+ " COMPANY_DATA"
+" WHERE"
+ " testing_id = 1234"
+" AND "
+ " company_employee_id = 3345";
int getCompanyID = session.createSQLQuery(selectCompanyID)
.executeUpdate();
System.out.println("GetCompanyID Test below");
System.out.println(getCompanyID);
String hqlInsert = "INSERT INTO Employee (NAME, IS_CORRECT,IS_WRONG,COMPANY_ID, TRANSACTION_ID,DEFINITION) VALUES"
+ "( SELECT value_emp_id FROM COMPANY_DATA WHERE testing_id = 1234 AND"
+ " company_employee_id = 3345))";
String hqlInsert = "INSERT INTO Employee (NAME,IS_CORRECT,IS_WRONG,COMPANY_ID,TRANSACTION_ID,DEFINITION) VALUES "
+ "("
+employee.getName()+","
+employee.getIsCorrect()+","
+employee.getIsWrong()+","
+getCompanyID+","
+employee.getTransactionId()+","
+employee.getDefinition()+")";
System.out.println("October 3 Checking for hqlInsert");
System.out.println(hqlInsert);
int createdEntities = session.createSQLQuery( hqlInsert )
.executeUpdate();
session.persist(employee);
tx.commit();
System.out.println("October 3 BELOW Checking for hqlInsert");
System.out.println(hqlInsert);
System.out.println("Checking for CreatedEntities");
System.out.println(createdEntities);
} catch(Exception ex) {
tx.rollback();
ex.printStackTrace();
status = false;
} finally {
session.close();
}
logger.debug("Completed EmployeeDaoImpl.insert() .....");
return status;
}
Console :
Hibernate:
SELECT
VALUE_EMP_ID
FROM
COMPANY_DATA
WHERE
testing_id = 1234
AND company_employee_id = 3345
GetCompanyID Test below
1
October 3 Checking for hqlInsert
INSERT INTO Employee (NAME,IS_CORRECT,IS_WRONG,COMPANY_ID,TRANSACTION_ID,DEFINITION) VALUES (Terminology,0,0,1,0,definitionTest)
Hibernate:
INSERT
INTO
Employee
(NAME,IS_CORRECT,IS_WRONG,COMPANY_ID,TRANSACTION_ID,DEFINITION)
VALUES
(Terminology,0,0,1,0,definitionTest)
2017-10-03 11:32:43.753 WARN 5392 --- [nio-8080-exec-4] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 984, SQLState: 42000
2017-10-03 11:32:43.753 ERROR 5392 --- [nio-8080-exec-4] o.h.engine.jdbc.spi.SqlExceptionHelper : ORA-00984: column not allowed here
Here is my Entity Class Employee.java is as follows:
package abc.def.mypackage.orm
#Entity
#Table(name = "EMPLOYEE")
public class Employee {
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getIsCorrect() {
return isCorrect;
}
public void setIsCorrect(int isCorrect) {
this.isCorrect = isCorrect;
}
public int getIsWrong() {
return isWrong;
}
public void setIsWrong(int isWrong) {
this.isWrong = isWrong;
}
public int getCompanyId() {
return companyId;
}
public void setCompanyId(int companyId) {
this.companyId = companyId;
}
public Integer getTransactionId() {
return transactionId;
}
public void setTransactionId(Integer transactionId) {
this.transactionId = transactionId;
}
public String getDefinition() {
return definition;
}
public void setDefinition(String definition) {
this.definition = definition;
}
#Id
#Column(name = "EMPLOYEE_ID")
#GeneratedValue(strategy = GenerationType.AUTO, generator = "seqgen")
#SequenceGenerator(name = "seqgen", sequenceName = "EMPLOYEE_AUTOINC_SEQ")
private int employeeId;
#Column(name = "NAME")
private String name;
#Column(name = "DEFINITION")
private String definition;
#Column(name = "IS_CORRECT")
private int isCorrect;
#Column(name = "IS_WRONG")
private int isWrong;
#Column(name = "COMPANY_ID")
private int companyId;
#Column(name = "TRANSACTION_ID", nullable = true)
private Integer transactionId;
}
Try putting your String values employee.getName() and employee.getDefinition() in your insert statement into quotes '
String hqlInsert = "INSERT INTO Employee (NAME,IS_CORRECT,IS_WRONG,COMPANY_ID,TRANSACTION_ID,DEFINITION) VALUES "
+ "('"
+employee.getName()+"',"
+employee.getIsCorrect()+","
+employee.getIsWrong()+","
+getCompanyID+","
+employee.getTransactionId()+",'"
+employee.getDefinition()+"')";

Hibernate CompositeUserType mapping has wrong number of columns

I am new to Hibernate. Writing a CompositeUserType. When I run the code I am getting error.
property
mapping has wrong number of columns:
Please help me what am I missing?
My CompositeUserType goes as follows
public class EncryptedAsStringType implements CompositeUserType {
#Override
public String[] getPropertyNames() {
return new String[] { "stockId", "stockCode", "stockName","stockDescription" };
}
#Override
public Type[] getPropertyTypes() {
//stockId, stockCode,stockName,modifiedDate
return new Type[] {
Hibernate.INTEGER, Hibernate.STRING, Hibernate.STRING,Hibernate.STRING
};
}
#Override
public Object getPropertyValue(final Object component, final int property)
throws HibernateException {
Object returnValue = null;
final Stock auditData = (Stock) component;
if (0 == property) {
returnValue = auditData.getStockId();
} else if (1 == property) {
returnValue = auditData.getStockCode();
} else if (2 == property) {
returnValue = auditData.getStockName();
} return returnValue;
}
#Override
public void setPropertyValue(final Object component, final int property,
final Object setValue) throws HibernateException {
final Stock auditData = (Stock) component;
}
#Override
public Object nullSafeGet(final ResultSet resultSet,
final String[] names,
final SessionImplementor paramSessionImplementor, final Object paramObject)
throws HibernateException, SQLException {
//owner here is of type TestUser or the actual owning Object
Stock auditData = null;
final Integer createdBy = resultSet.getInt(names[0]);
//Deferred check after first read
if (!resultSet.wasNull()) {
auditData = new Stock();
System.out.println(">>>>>>>>>>>>"+resultSet.getInt(names[1]));
System.out.println(">>>>>>>>>>>>"+resultSet.getString(names[2]));
System.out.println(">>>>>>>>>>>>"+resultSet.getString(names[3]));
System.out.println(">>>>>>>>>>>>"+resultSet.getString(names[4]));
}
return auditData;
}
#Override
public void nullSafeSet(final PreparedStatement preparedStatement,
final Object value, final int property,
final SessionImplementor sessionImplementor)
throws HibernateException, SQLException {
if (null == value) {
} else {
final Stock auditData = (Stock) value;
System.out.println("::::::::::::::::::::::::::::::::"+auditData.getStockCode());
System.out.println("::::::::::::::::::::::::::::::::"+auditData.getStockDescription());
System.out.println("::::::::::::::::::::::::::::::::"+auditData.getStockId());
System.out.println("::::::::::::::::::::::::::::::::"+auditData.getStatus());
}
}
My Domain class Stock has five attributes. (stockId,stockCode,StockName,Status , Stock
Description)
I need to declare the field Stock description as Composite field Type.
private Integer stockId;
private String stockCode;
private String stockName;
private String status;
private String stockDescription;
//Constructors
#Column(name = "STOCK_CC", unique = true, nullable = false, length = 20)
#Type(type="com.mycheck.EncryptedAsStringType")
#Columns(columns = { #Column(name="STOCK_ID"),
#Column(name="STOCK_CODE"),
#Column(name="STOCK_NAME")
})
public String getStockDescription() {
return stockDescription;
}
}
When I try to execute a insert for Stock. I am getting the error Error creating bean with name
'sessionFactory' defined in class path resource [spring/config/../database/Hibernate.xml]:
Invocation of init method failed. nested exception is org.hibernate.MappingException:
property mapping has wrong number of columns: com.stock.model.Stock.stockDescription type:
com.mycheck.EncryptedAsStringType
Where am I going wrong ?
One can extract the answer from the code samples and the comments to the original question, but to save everyone some reading, I've compiled a quick summary.
If you declare a CompositeUserType that maps a type to n columns, you have to declare n columns in #Columns besides the #Type annotation. Example:
public class EncryptedAsStringType implements CompositeUserType {
#Override
public String[] getPropertyNames() {
return new String[] { "stockId", "stockCode", "stockName","stockDescription" };
}
// ...
}
This CompositeUserType maps to 4 separate columns, therefore 4 separate #Column annotations have to be declared:
#Type(type="com.mycheck.EncryptedAsStringType")
#Columns(columns = {
#Column(name="STOCK_ID"),
#Column(name="STOCK_CODE"),
#Column(name="STOCK_NAME"),
#Column(name="STOCK_DESCRIPTION")
})
public String getStockDescription() {
return stockDescription;
}
That's it and Hibernate is happy.

Resources