Note that there are some explanatory texts on larger screens.

plurals
  1. POMapping CHAR(0) in MySQL to boolean in Hibernate
    primarykey
    data
    text
    <p>I have a table named <code>refund_rule</code> in mysql<i>(version 5.5)</i>. Here is it's definition:</p> <pre><code>CREATE TABLE `refund_rule` ( `ID` int(10) unsigned NOT NULL AUTO_INCREMENT, `PERCENTAGE` char(0) DEFAULT NULL COMMENT 'BOOLEAN shortcut.NULL&lt;=&gt;false,EMPTY&lt;=&gt;true', `DEDUCTION_AMOUNT` int(10) unsigned DEFAULT NULL, PRIMARY KEY (`ID`) ); </code></pre> <p>The corresponding class in Hibernate<i>(version 3.2)</i> is named <code>RefundRule</code>. The HBM file looks like this:</p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt; &lt;hibernate-mapping&gt; &lt;class name="hibernatesample.dao.RefundRule" table="refund_rule" catalog="back_end_proc"&gt; &lt;id name="id" type="java.lang.Integer"&gt; &lt;column name="ID" /&gt; &lt;generator class="identity" /&gt; &lt;/id&gt; &lt;property name="percentage" type="string"&gt; &lt;column name="PERCENTAGE" length="0"&gt; &lt;comment&gt;BOOLEAN shortcut.NULL&amp;lt;=&amp;gt;false,EMPTY&amp;lt;=&amp;gt;true&lt;/comment&gt; &lt;/column&gt; &lt;/property&gt; &lt;property name="deductionAmount" type="java.lang.Integer"&gt; &lt;column name="DEDUCTION_AMOUNT" /&gt; &lt;/property&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>The class generated by the wizard in NetBeans<i>(version 7.0)</i> was this:</p> <pre><code>public class RefundRule implements java.io.Serializable { private Integer id; private String percentage; private Integer deductionAmount; public CancellationRule() { } public CancellationRule(String percentage, Integer deductionAmount) { this.percentage = percentage; this.deductionAmount = deductionAmount; } public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public String getPercentage() { return this.percentage; } public void setPercentage(String percentage) { this.percentage = percentage; } public Integer getDeductionAmount() { return this.deductionAmount; } public void setDeductionAmount(Integer deductionAmount) { this.deductionAmount = deductionAmount; } } </code></pre> <p>I added 2 more methods in it <code>setPercentage(boolean)</code> &amp; <code>isPercentage()</code>, and changed the method <code>setPercentage(String)</code> , so that i can use that <code>String</code> object as <code>boolean</code> in my Java<i>(version 1.6)</i> program.</p> <pre><code>public class RefundRule implements java.io.Serializable { . . . public String getPercentage() { return this.percentage; } public void setPercentage(String percentage) { this.percentage = percentage==null?null:""; } . . . public void setPercentage(boolean percentage){ setPercentage(percentage?"":null); } public boolean isPercentage(){ return percentage!=null; } } </code></pre> <p>My Question is:</p> <p>Is there any way that I can keep only two methods: <code>setPercentage(boolean)</code> and <code>isPercentage()</code> , and map the <code>boolean percentage</code> variable to <code>PERCENTAGE CHAR(0)</code> variable in mysql.</p> <p>===================================================================</p> <p><b> EDIT </b> added on <i> 2013-11-23 </i></p> <p>Following the answer by @GreyBeardedGeek , I made following changes in code:</p> <p>(Changes in brief)</p> <ol> <li>Added Class CharToBoolUserType</li> <li>Changed the <code>type</code>-attribute of hbm-element: <i>percentage</i> in RefundRule.hbm.xml</li> </ol> <p>(Code related to above mentioned changes)</p> <ol> <li><p>The class CharToBoolUserType:</p> <pre><code>import java.io.Serializable; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Types; import org.hibernate.HibernateException; import org.hibernate.usertype.UserType; public class CharToBoolUserType implements UserType { private static final int[] SQL_TYPES = {Types.CHAR}; @Override public Object assemble(Serializable serializable, Object object) throws HibernateException { return serializable; } @Override public Object deepCopy(Object object) throws HibernateException { return object; } @Override public Serializable disassemble(Object value) throws HibernateException { return (Serializable) value; } @Override public boolean equals(Object x, Object y) throws HibernateException { if (x == y) { return true; } else if (x == null || y == null) { return false; } else { return x.equals(y); } } @Override public boolean isMutable() { return false; } @Override public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner) throws HibernateException, SQLException { return resultSet.getObject(names[0]) != null; } @Override public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index) throws HibernateException, SQLException { preparedStatement.setObject(index, ((Boolean) value).booleanValue() ? "" : null); } @Override public Object replace(Object original, Object target, Object owner) throws HibernateException { return original; } @Override public Class returnedClass() { return boolean.class; } @Override public int[] sqlTypes() { return SQL_TYPES; } @Override public int hashCode(Object object) throws HibernateException { if (object == null) { return 0; } // is `object` a String ? Or boolean? return 1; } } </code></pre></li> <li><p>The file RefundRule.hbm.xml:</p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt; &lt;hibernate-mapping&gt; &lt;class name="hibernatesample.dao.RefundRule" table="refund_rule" catalog="back_end_proc"&gt; &lt;id name="id" type="java.lang.Integer"&gt; &lt;column name="ID" /&gt; &lt;generator class="identity" /&gt; &lt;/id&gt; &lt;property name="percentage" type="hibernatesample.dao.CharToBoolUserType"&gt; &lt;column name="PERCENTAGE" length="0"&gt; &lt;comment&gt;BOOLEAN shortcut.NULL&amp;lt;=&amp;gt;false,EMPTY&amp;lt;=&amp;gt;true&lt;/comment&gt; &lt;/column&gt; &lt;/property&gt; &lt;property name="deductionAmount" type="java.lang.Integer"&gt; &lt;column name="DEDUCTION_AMOUNT" /&gt; &lt;/property&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre></li> </ol> <p>Since I want the code of the <code>class CharToBoolUserType</code> to be complete in all sense, I have following questions:<br> 1. What is the class of <code>object</code> in <code>hashCode(Object object)</code>, <code>Boolean</code> or <code>String</code>? Who calls this method?<br> 2. What the method <code>public Object replace(Object original, Object target, Object owner)</code> supposed to do? replace <code>original</code> with <code>target</code> and set/put it in <code>owner</code>. In this case: is <code>original</code> of <code>String</code> type, <code>target</code> of <code>Boolean</code> type, and <code>owner</code> of <code>RefundRule</code> type ?</p> <p>Any suggestion(s) to improve this code is welcome.</p> <p>=================================================</p> <p>Just for reference, the class RefundRule is like this now:</p> <pre><code>public class RefundRule implements java.io.Serializable { private Integer id; private boolean percentage; private Integer deductionAmount; public RefundRule() { } public RefundRule(boolean percentage, Integer deductionAmount) { this.percentage = percentage; this.deductionAmount = deductionAmount; } public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public Integer getDeductionAmount() { return this.deductionAmount; } public void setDeductionAmount(Integer deductionAmount) { this.deductionAmount = deductionAmount; } public void setPercentage(boolean percentage){ this.percentage=percentage; } public boolean isPercentage(){ return percentage; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload