当前位置:   article > 正文

JPA注解:根据实体生成数据表和字段的注释(正向工程)_jpa 表注释和字段注释_jpa注释–hibernate注释

jpa 表注释和字段注释_jpa注释–hibernate注释
1.JPA常见注解
   
   
  1. 请移步:http://blog.csdn.net/fly910905/article/details/78140411

2.JPA注解:表注释
   
   
  1. @org.hibernate.annotations.Table(appliesTo = "TableName",comment="表注释")
    
    
  1. /*
  2. * Hibernate, Relational Persistence for Idiomatic Java
  3. *
  4. * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
  5. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  6. */
  7. package org.hibernate.annotations;
  8. import java.lang.annotation.Retention;
  9. import java.lang.annotation.Target;
  10. import static java.lang.annotation.ElementType.TYPE;
  11. import static java.lang.annotation.RetentionPolicy.RUNTIME;
  12. /**
  13. * Complementary information to a table either primary or secondary.
  14. *
  15. * @author Emmanuel Bernard
  16. */
  17. @Target({TYPE})
  18. @Retention(RUNTIME)
  19. public @interface Table {
  20. /**
  21. * name of the targeted table.
  22. */
  23. String appliesTo();
  24. /**
  25. * Indexes.
  26. */
  27. Index[] indexes() default {};
  28. /**
  29. * define a table comment.
  30. */
  31. String comment() default "";
  32. /**
  33. * Defines the Foreign Key name of a secondary table pointing back to the primary table.
  34. */
  35. ForeignKey foreignKey() default @ForeignKey( name="" );
  36. /**
  37. * If set to JOIN, the default, Hibernate will use an inner join to retrieve a
  38. * secondary table defined by a class or its superclasses and an outer join for a
  39. * secondary table defined by a subclass.
  40. * If set to select then Hibernate will use a
  41. * sequential select for a secondary table defined on a subclass, which will be issued only if a row
  42. * turns out to represent an instance of the subclass. Inner joins will still be used to retrieve a
  43. * secondary defined by the class and its superclasses.
  44. *
  45. * <b>Only applies to secondary tables</b>
  46. */
  47. FetchMode fetch() default FetchMode.JOIN;
  48. /**
  49. * If true, Hibernate will not try to insert or update the properties defined by this join.
  50. *
  51. * <b>Only applies to secondary tables</b>
  52. */
  53. boolean inverse() default false;
  54. /**
  55. * If enabled, Hibernate will insert a row only if the properties defined by this join are non-null
  56. * and will always use an outer join to retrieve the properties.
  57. *
  58. * <b>Only applies to secondary tables</b>
  59. */
  60. boolean optional() default true;
  61. /**
  62. * Defines a custom SQL insert statement.
  63. *
  64. * <b>Only applies to secondary tables</b>
  65. */
  66. SQLInsert sqlInsert() default @SQLInsert(sql="");
  67. /**
  68. * Defines a custom SQL update statement.
  69. *
  70. * <b>Only applies to secondary tables</b>
  71. */
  72. SQLUpdate sqlUpdate() default @SQLUpdate(sql="");
  73. /**
  74. * Defines a custom SQL delete statement.
  75. *
  76. * <b>Only applies to secondary tables</b>
  77. */
  78. SQLDelete sqlDelete() default @SQLDelete(sql="");
  79. }


3.JPA注解:字段注释
    
    
  1. @Column(name="columnComment",columnDefinition="varchar(200) COMMENT '字段注释'")
     
     
  1. /*
  2. * Copyright (c) 2008, 2009, 2011 Oracle, Inc. All rights reserved.
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
  6. * which accompanies this distribution. The Eclipse Public License is available
  7. * at http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution License
  8. * is available at http://www.eclipse.org/org/documents/edl-v10.php.
  9. */
  10. package javax.persistence;
  11. import java.lang.annotation.Retention;
  12. import java.lang.annotation.Target;
  13. import static java.lang.annotation.ElementType.FIELD;
  14. import static java.lang.annotation.ElementType.METHOD;
  15. import static java.lang.annotation.RetentionPolicy.RUNTIME;
  16. /**
  17. * Is used to specify the mapped column for a persistent property or field.
  18. * If no <code>Column</code> annotation is specified, the default values apply.
  19. *
  20. * <blockquote><pre>
  21. * Example 1:
  22. *
  23. * &#064;Column(name="DESC", nullable=false, length=512)
  24. * public String getDescription() { return description; }
  25. *
  26. * Example 2:
  27. *
  28. * &#064;Column(name="DESC",
  29. * columnDefinition="CLOB NOT NULL",
  30. * table="EMP_DETAIL")
  31. * &#064;Lob
  32. * public String getDescription() { return description; }
  33. *
  34. * Example 3:
  35. *
  36. * &#064;Column(name="ORDER_COST", updatable=false, precision=12, scale=2)
  37. * public BigDecimal getCost() { return cost; }
  38. *
  39. * </pre></blockquote>
  40. *
  41. *
  42. * @since Java Persistence 1.0
  43. */
  44. @Target({METHOD, FIELD})
  45. @Retention(RUNTIME)
  46. public @interface Column {
  47. /**
  48. * (Optional) The name of the column. Defaults to
  49. * the property or field name.
  50. */
  51. String name() default "";
  52. /**
  53. * (Optional) Whether the column is a unique key. This is a
  54. * shortcut for the <code>UniqueConstraint</code> annotation at the table
  55. * level and is useful for when the unique key constraint
  56. * corresponds to only a single column. This constraint applies
  57. * in addition to any constraint entailed by primary key mapping and
  58. * to constraints specified at the table level.
  59. */
  60. boolean unique() default false;
  61. /**
  62. * (Optional) Whether the database column is nullable.
  63. */
  64. boolean nullable() default true;
  65. /**
  66. * (Optional) Whether the column is included in SQL INSERT
  67. * statements generated by the persistence provider.
  68. */
  69. boolean insertable() default true;
  70. /**
  71. * (Optional) Whether the column is included in SQL UPDATE
  72. * statements generated by the persistence provider.
  73. */
  74. boolean updatable() default true;
  75. /**
  76. * (Optional) The SQL fragment that is used when
  77. * generating the DDL for the column.
  78. * <p> Defaults to the generated SQL to create a
  79. * column of the inferred type.
  80. */
  81. String columnDefinition() default "";
  82. /**
  83. * (Optional) The name of the table that contains the column.
  84. * If absent the column is assumed to be in the primary table.
  85. */
  86. String table() default "";
  87. /**
  88. * (Optional) The column length. (Applies only if a
  89. * string-valued column is used.)
  90. */
  91. int length() default 255;
  92. /**
  93. * (Optional) The precision for a decimal (exact numeric)
  94. * column. (Applies only if a decimal column is used.)
  95. * Value must be set by developer if used when generating
  96. * the DDL for the column.
  97. */
  98. int precision() default 0;
  99. /**
  100. * (Optional) The scale for a decimal (exact numeric) column.
  101. * (Applies only if a decimal column is used.)
  102. */
  103. int scale() default 0;
  104. }

4.示例:

4.1实体(实体中使用了Swagger,不需要的可去掉):
     
     
  1. package com.newcapec.dao.domain;
  2. import io.swagger.annotations.ApiModel;
  3. import io.swagger.annotations.ApiModelProperty;
  4. import javax.persistence.Column;
  5. import javax.persistence.Entity;
  6. import javax.persistence.GeneratedValue;
  7. import javax.persistence.Id;
  8. import java.util.Date;
  9. import static javax.persistence.GenerationType.AUTO;
  10. /**
  11. * @Title: 用户报名缴费信息实体
  12. * @ClassName: com.newcapec.dao.domain.UserEnrollPay.java
  13. * @Description: 位于校内的报名系统--中间库上
  14. *
  15. * @Copyright 2016-2017 新开普 - Powered By 研发中心
  16. * @author: 王延飞
  17. * @date: 2017-12-13 15:31
  18. * @version V1.0
  19. */
  20. @Entity
  21. @org.hibernate.annotations.Table(appliesTo = "user_enroll_pay",comment="用户报名缴费信息")
  22. @ApiModel("Person(用户报名缴费信息)")
  23. public class UserEnrollPay {
  24. @Id
  25. @GeneratedValue(strategy = AUTO)
  26. @Column(name = "id", unique = true, nullable = false,columnDefinition="bigint(20) COMMENT '主键id'")
  27. @ApiModelProperty("主键id")
  28. private Long id;
  29. /**
  30. * 证件号
  31. */
  32. @Column(columnDefinition="varchar(20) COMMENT '证件号'")
  33. @ApiModelProperty("证件号")
  34. private String idserial;
  35. /**
  36. * 订单金额
  37. */
  38. @Column(columnDefinition = "Decimal(10,2) COMMENT '订单金额'", scale = 2 ,precision=10)
  39. @ApiModelProperty("订单金额")
  40. private double orderamt;
  41. /**
  42. * 支付时间
  43. */
  44. @Column(columnDefinition="datetime COMMENT '支付时间'")
  45. @ApiModelProperty("支付时间")
  46. private Date paytime;
  47. /**
  48. * 支付标志
  49. * 0-未支付
  50. * 1-支付中
  51. * 2-支付成功
  52. * 3-支付失败
  53. * 4-已过期
  54. * 5-已取消
  55. */
  56. @Column(columnDefinition="int(2) COMMENT '支付标志 0-未支付 1-支付中 2-支付成功 3-支付失败 4-已过期 5-已取消'")
  57. @ApiModelProperty("支付标志 0-未支付 1-支付中 2-支付成功 3-支付失败 4-已过期 5-已取消")
  58. private Integer payflag;
  59. public Long getId() {
  60. return id;
  61. }
  62. public void setId(Long id) {
  63. this.id = id;
  64. }
  65. public String getIdserial() {
  66. return idserial;
  67. }
  68. public void setIdserial(String idserial) {
  69. this.idserial = idserial;
  70. }
  71. public double getOrderamt() {
  72. return orderamt;
  73. }
  74. public void setOrderamt(double orderamt) {
  75. this.orderamt = orderamt;
  76. }
  77. public Date getPaytime() {
  78. return paytime;
  79. }
  80. public void setPaytime(Date paytime) {
  81. this.paytime = paytime;
  82. }
  83. public Integer getPayflag() {
  84. return payflag;
  85. }
  86. public void setPayflag(Integer payflag) {
  87. this.payflag = payflag;
  88. }
  89. @Override
  90. public String toString() {
  91. return "UserEnrollPay{" +
  92. "id=" + id +
  93. ", idserial='" + idserial + '\'' +
  94. ", orderamt=" + orderamt +
  95. ", paytime=" + paytime +
  96. ", payflag=" + payflag +
  97. '}';
  98. }
  99. }

4.2生成的数据表
      
      
  1. CREATE TABLE `user_enroll_pay` (
  2. `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键id',
  3. `idserial` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '证件号',
  4. `orderamt` decimal(10,2) DEFAULT NULL COMMENT '订单金额',
  5. `payflag` int(2) DEFAULT NULL COMMENT '支付标志 0-未支付 1-支付中 2-支付成功 3-支付失败 4-已过期 5-已取消',
  6. `paytime` datetime DEFAULT NULL COMMENT '支付时间',
  7. PRIMARY KEY (`id`)
  8. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='用户报名缴费信息';
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/530912
推荐阅读
相关标签
  

闽ICP备14008679号