歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Hibernate 客戶化映射類型--UserType

Hibernate 的客戶化映射類型接口,允許用戶以編程的方式創建自定義的映射類型,以實現把持久化類的任意類型的屬性映射到數據庫
以下例子可以把User類的Integer類型的phone屬性映射到user表的varchar類型的phone字段

User.java

  1. package com.wudasong.pojo;
  2. import java.io.Serializable;
  3. import java.sql.Timestamp;
  4. import java.util.Date;
  5. publicclass User implements Serializable{
  6. /**
  7. *
  8. */
  9. privatestaticfinallong serialVersionUID = 5767515928671889957L;
  10. private Long id;
  11. private String name;
  12. private String email;
  13. private Integer phone;
  14. privatebyte sex;
  15. private Date birthday;
  16. private String mark;
  17. private String password;
  18. private Timestamp registerTime;
  19. public User(){}
  20. public Long getId() {
  21. return id;
  22. }
  23. @SuppressWarnings("unused")
  24. privatevoid setId(Long id) {
  25. this.id = id;
  26. }
  27. //此處省略User類的其他屬性的getXXX()和setXXX()方法
  28. }

自定義UserType類

  1. package com.wudasong.pojo;
  2. import java.io.Serializable;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Types;
  7. import org.hibernate.HibernateException;
  8. import org.hibernate.usertype.UserType;
  9. publicclass PhoneUserType implements UserType{
  10. privatestaticfinalint[] SQL_TYPES={Types.VARCHAR};
  11. @Override
  12. publicint[] sqlTypes() {
  13. return SQL_TYPES;
  14. }
  15. @Override
  16. public Class<Integer> returnedClass() {
  17. return Integer.class;
  18. }
  19. @Override
  20. publicboolean equals(Object x, Object y) throws HibernateException {
  21. if(x==y){
  22. returntrue;
  23. }elseif(x==null||y==null){
  24. returnfalse;
  25. }else {
  26. return x.equals(y);
  27. }
  28. }
  29. @Override
  30. publicint hashCode(Object x) throws HibernateException {
  31. return x.hashCode();
  32. }
  33. @Override
  34. public Object deepCopy(Object value) throws HibernateException {
  35. return value;
  36. }
  37. @Override
  38. publicboolean isMutable() {
  39. returnfalse;
  40. }
  41. @Override
  42. public Serializable disassemble(Object value) throws HibernateException {
  43. return (Serializable) value;
  44. }
  45. @Override
  46. public Object assemble(Serializable cached, Object owner)
  47. throws HibernateException {
  48. return cached;
  49. }
  50. @Override
  51. public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner)
  52. throws HibernateException, SQLException {
  53. if(resultSet.wasNull()){
  54. returnnull;
  55. }else{
  56. String phone=resultSet.getString(names[0]);
  57. returnnew Integer(phone);
  58. }
  59. }
  60. @Override
  61. publicvoid nullSafeSet(PreparedStatement statement, Object value, int index)
  62. throws HibernateException, SQLException {
  63. if(value==null){
  64. statement.setNull(index, Types.VARCHAR);
  65. }else {
  66. String phone=((Integer)value).toString();
  67. statement.setString(index, phone);
  68. }
  69. }
  70. @Override
  71. public Object replace(Object original, Object target, Object owner)
  72. throws HibernateException {
  73. return original;
  74. }
Copyright © Linux教程網 All Rights Reserved