开发者

HibernateTools Reverse Engineering tool does not add Annotation for generator

开发者 https://www.devze.com 2023-02-05 20:07 出处:网络
I have my MySQL DB Schema created and I am using Hibernate Reverse Engineering file to create annotated Domain Object (.java). Though the file is generated correctly, it is somehow missing the \"Gener

I have my MySQL DB Schema created and I am using Hibernate Reverse Engineering file to create annotated Domain Object (.java). Though the file is generated correctly, it is somehow missing the "Generator" annotation for ID field.

Below is my hibernate.reveng.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE
hibernate-reverse-engineering PUBLIC
"-//Hibernate/Hibernate Reverse
Engineering DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd"
<hibernate-reverse-engineering>
  <table-filter match-name="products" match-catalog="test"></table-filter>
  <table catalog="test" name="products">
    <primary-key>
      <generator class="native"></generator>
      <key-column name="product_id"property="product_id" />
    </primary-key> 
  </table>
</hibernate-reverse-engineering>

and the generated class file (Products.java):

// default package
// Generated Jan 21, 2011 8:27:16 PM by Hibernate Too开发者_JAVA技巧ls 3.3.0.GA

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * Products generated by hbm2java
 */
@Entity
@Table(name = "products", catalog = "test")
public class Products implements java.io.Serializable {

 private String productId;
 private String productName;

 public Products() {
 }

 public Products(String productId) {
  this.productId = productId;
 }

 public Products(String productId, String productName) {
  this.productId = productId;
  this.productName = productName;
 }

 @Id
 @Column(name = "product_id", unique = true, nullable = false, length = 50)
 public String getProductId() {
  return this.productId;
 }

 public void setProductId(String productId) {
  this.productId = productId;
 }

 @Column(name = "product_name", length = 200)
 public String getProductName() {
  return this.productName;
 }

 public void setProductName(String productName) {
  this.productName = productName;
 }

}

Is there something missing in my hibernate.reveng.xml file or hibernate does not generate annotation for "generator"?


you need to check "ejb3" or add in the config:

<hbm2java  jdk5="true" ejb3="true" />


  <key-column name="product_id" property="product_id" />

There's a problem here. This part is correct: key-column name="product_id", it maps to the DB column product_id, but this part is wrong: property="product_id", this is the Java property and that is called productId, not product_id. This is the correct value:

  <key-column name="product_id" property="productId" />

And yes: AFAIK auto-generation is only possible for numeric types.

0

精彩评论

暂无评论...
验证码 换一张
取 消