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

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 .
  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. * Only applies to secondary tables
  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. * Only applies to secondary tables
  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. * Only applies to secondary tables
  59. */
  60. boolean optional() default true;
  61. /**
  62. * Defines a custom SQL insert statement.
  63. *
  64. * Only applies to secondary tables
  65. */
  66. SQLInsert sqlInsert() default @SQLInsert(sql="");
  67. /**
  68. * Defines a custom SQL update statement.
  69. *
  70. * Only applies to secondary tables
  71. */
  72. SQLUpdate sqlUpdate() default @SQLUpdate(sql="");
  73. /**
  74. * Defines a custom SQL delete statement.
  75. *
  76. * Only applies to secondary tables
  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 Column annotation is specified, the default values apply.
  19. *
  20. *
  21. * Example 1:
  22. *
  23. * @Column(name="DESC", nullable=false, length=512)
  24. * public String getDescription() { return description; }
  25. *
  26. * Example 2:
  27. *
  28. * @Column(name="DESC",
  29. * columnDefinition="CLOB NOT NULL",
  30. * table="EMP_DETAIL")
  31. * @Lob
  32. * public String getDescription() { return description; }
  33. *
  34. * Example 3:
  35. *
  36. * @Column(name="ORDER_COST", updatable=false, precision=12, scale=2)
  37. * public BigDecimal getCost() { return cost; }
  38. *
  39. *
  • *
  • *
  • * @since Java Persistence 1.0
  • */
  • @Target({METHOD, FIELD})
  • @Retention(RUNTIME)
  • public @interface Column {
  • /**
  • * (Optional) The name of the column. Defaults to
  • * the property or field name.
  • */
  • String name() default "";
  • /**
  • * (Optional) Whether the column is a unique key. This is a
  • * shortcut for the UniqueConstraint annotation at the table
  • * level and is useful for when the unique key constraint
  • * corresponds to only a single column. This constraint applies
  • * in addition to any constraint entailed by primary key mapping and
  • * to constraints specified at the table level.
  • */
  • boolean unique() default false;
  • /**
  • * (Optional) Whether the database column is nullable.
  • */
  • boolean nullable() default true;
  • /**
  • * (Optional) Whether the column is included in SQL INSERT
  • * statements generated by the persistence provider.
  • */
  • boolean insertable() default true;
  • /**
  • * (Optional) Whether the column is included in SQL UPDATE
  • * statements generated by the persistence provider.
  • */
  • boolean updatable() default true;
  • /**
  • * (Optional) The SQL fragment that is used when
  • * generating the DDL for the column.
  • *

    Defaults to the generated SQL to create a

  • * column of the inferred type.
  • */
  • String columnDefinition() default "";
  • /**
  • * (Optional) The name of the table that contains the column.
  • * If absent the column is assumed to be in the primary table.
  • */
  • String table() default "";
  • /**
  • * (Optional) The column length. (Applies only if a
  • * string-valued column is used.)
  • */
  • int length() default 255;
  • /**
  • * (Optional) The precision for a decimal (exact numeric)
  • * column. (Applies only if a decimal column is used.)
  • * Value must be set by developer if used when generating
  • * the DDL for the column.
  • */
  • int precision() default 0;
  • /**
  • * (Optional) The scale for a decimal (exact numeric) column.
  • * (Applies only if a decimal column is used.)
  • */
  • int scale() default 0;
  • }

  • 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='用户报名缴费信息';

    你可能感兴趣的:(JPA,Hibernate)