From 8b3bccca75ae36f836d28ab725cf963c10a9486e Mon Sep 17 00:00:00 2001 From: goik <goik@hdm-stuttgart.de> Date: Sun, 17 Feb 2013 20:14:55 +0100 Subject: [PATCH] type mapping modification --- Doc/course.xml | 58 ++++++++++++++++++- .../java/hibintro/v9/PersistSingleUser.java | 33 +++++++++++ .../src/main/java/hibintro/v9/User.java | 47 +++++++++++++++ .../main/java/hibintro/v9/hibernate.cfg.xml | 17 ++++++ 4 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 ws/eclipse/HibIntro/src/main/java/hibintro/v9/PersistSingleUser.java create mode 100644 ws/eclipse/HibIntro/src/main/java/hibintro/v9/User.java create mode 100644 ws/eclipse/HibIntro/src/main/java/hibintro/v9/hibernate.cfg.xml diff --git a/Doc/course.xml b/Doc/course.xml index 7e4104ee0..3399a7007 100644 --- a/Doc/course.xml +++ b/Doc/course.xml @@ -16484,7 +16484,63 @@ public class User { <section xml:id="sectChangeDefaultTypeMapping"> <title>Changing the default type mapping</title> - <para/> + <para>Sometimes we are interested in changing <abbrev + xlink:href="http://www.javaworld.com/javaworld/jw-01-2008/jw-01-jpa1.html">JPA</abbrev>'s + default type mapping strategy. For example <trademark + xlink:href="http://www.mysql.com/about/legal/trademark.html">Mysql</trademark> + versions prior to 5.0 lack an appropriate type representing boolean + values. It was therefore quite common mapping boolean properties to + <code>CHAR(1)</code> with possible values being <code>'Y'</code> and + <code>'N'</code>. Hibernate will map boolean values to + <code>tinyint(1)</code>. Supporting older software may require to + tweak the standard mapping.</para> + + <para>Unfortunately <abbrev + xlink:href="http://www.javaworld.com/javaworld/jw-01-2008/jw-01-jpa1.html">JPA</abbrev> + itself does not offer any interface for this purpose. The + persistence provider may offer a solution though. Hibernate for + example allows to remap <coref linkend="remapBooleanChar"/> types . + We assume our <classname>hibintro.v9.User</classname> class to have + a <code>boolean</code> property <code>active</code>:</para> + + <informaltable border="1"> + <colgroup width="6%"/> + + <colgroup width="94%"/> + + <tr> + <td valign="top"><emphasis role="bold">Java</emphasis></td> + + <td valign="top"><programlisting>package hibintro.v9; +... +public class User { +... + public void setCname(String cname) {this.cname = cname;} + + boolean active = false; + @Type(type="yes_no") <co xml:id="remapBooleanChar"/> + public boolean isActive() {return active;} + public void setActive(boolean active) {this.active = active;} +}</programlisting></td> + </tr> + + <tr> + <td valign="top"><emphasis role="bold">Sql</emphasis></td> + + <td><programlisting>CREATE TABLE User ( + uidNumber int(11) NOT NULL PRIMARY KEY, + active char(1) NOT NULL, + cname varchar(255) DEFAULT NULL, + uid varchar(255) NOT NULL +)</programlisting></td> + </tr> + </informaltable> + + <para>Readers being interested in more sophisticated strategies like + mapping user defined datatypes to database types are advised to read + the <link + xlink:href="http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html/ch05.html#mapping-types">manual + section on Hibernate types</link>.</para> </section> </section> diff --git a/ws/eclipse/HibIntro/src/main/java/hibintro/v9/PersistSingleUser.java b/ws/eclipse/HibIntro/src/main/java/hibintro/v9/PersistSingleUser.java new file mode 100644 index 000000000..1ce093231 --- /dev/null +++ b/ws/eclipse/HibIntro/src/main/java/hibintro/v9/PersistSingleUser.java @@ -0,0 +1,33 @@ +package hibintro.v9; + +import hibintro.util.HibernateUtil; + +import org.hibernate.Session; +import org.hibernate.Transaction; + +/** + * @author goik + * + * Persisting a simple instance of {@link User} + * + */ +public class PersistSingleUser { + + /** + * @param args not used. + */ + public static void main(String[] args) { + final Session session = HibernateUtil.createSessionFactory("hibintro/v9/hibernate.cfg.xml").openSession(); + + final Transaction transaction = session.beginTransaction(); + { + final User u = new User(); + u.setUidNumber(123); + u.setUid("goik"); + u.setCname("Martin Goik"); + + session.save(u); + } + transaction.commit(); + } +} \ No newline at end of file diff --git a/ws/eclipse/HibIntro/src/main/java/hibintro/v9/User.java b/ws/eclipse/HibIntro/src/main/java/hibintro/v9/User.java new file mode 100644 index 000000000..0cd019f04 --- /dev/null +++ b/ws/eclipse/HibIntro/src/main/java/hibintro/v9/User.java @@ -0,0 +1,47 @@ +package hibintro.v9; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; + +import org.hibernate.annotations.Type; + +/** + * @author goik + * + * {@link User} instances with re-mapped boolean property + */ +@Entity +public class User { + + int uidNumber; + @Id + public int getUidNumber() {return uidNumber;} + public void setUidNumber(int uidNumber) {this.uidNumber = uidNumber;} + + String uid; + /** + * @return The user's unique login name e.g. "goik" + */ + @Column(nullable=false) + public String getUid() {return uid;} + /** + * @param uid See {@link #getUid()}. + */ + public void setUid(String uid) {this.uid = uid;} + + String cname; + /** + * @return The user's common name e.g. "Martin Goik" + */ + public String getCname() {return cname;} + /** + * @param cname See {@link #getCname()}. + */ + public void setCname(String cname) {this.cname = cname;} + + boolean active = false; + @Type(type="yes_no") + public boolean isActive() {return active;} + public void setActive(boolean active) {this.active = active;} +} \ No newline at end of file diff --git a/ws/eclipse/HibIntro/src/main/java/hibintro/v9/hibernate.cfg.xml b/ws/eclipse/HibIntro/src/main/java/hibintro/v9/hibernate.cfg.xml new file mode 100644 index 000000000..e4caa9fa7 --- /dev/null +++ b/ws/eclipse/HibIntro/src/main/java/hibintro/v9/hibernate.cfg.xml @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" + "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> +<hibernate-configuration> + <session-factory > + <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> + <property name="hibernate.connection.password">XYZ</property> + <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hdm</property> + <property name="hibernate.connection.username">hdmuser</property> + <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> + <property name="hibernate.show_sql">true</property> + <property name="hibernate.format_sql">true</property> + <property name="hibernate.hbm2ddl.auto">update</property> + + <mapping class="hibintro.v9.User"/> + </session-factory> +</hibernate-configuration> -- GitLab