2011-01-21 2 views
5

Ho creato il mio schema DB MySQL e sto utilizzando il file Hibernate Reverse Engineering per creare l'oggetto dominio annotato (.java). Sebbene il file sia generato correttamente, manca in qualche modo l'annotazione "Generatore" per il campo ID.Lo strumento Reverse Engineering di HibernateTools non aggiunge annotazioni per il generatore

Qui di seguito è il mio 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> 

e il file di classe generata (Products.java):

// default package 
// Generated Jan 21, 2011 8:27:16 PM by Hibernate Tools 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; 
} 

} 

C'è qualcosa che manca nel mio file hibernate.reveng.xml o l'ibernazione non genera annotazioni per "generatore"?

+0

Dopo qualche ricerca, ho seguito un po 'di aiuto e ha fatto seguenti correzioni: 1) Aggiornamento product_id tipo di dati della colonna db a INT 2) AUTO_INCREMENT Assegnato attributo. Ora lo strumento è in grado di generare annotazioni per Native Generator come "@GeneratedValue" in Domain Object (Products.java). Devo ancora verificare se funziona solo per le colonne INT o per le colonne con altri tipi di dati e come farlo funzionare per le colonne VARCHAR. – mayur

risposta

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

C'è un problema qui. Questa parte è corretta: key-column name="product_id", si associa alla colonna DB product_id, ma questa parte è errata: property="product_id", questa è la proprietà Java e si chiama productId, non product_id. Questo è il valore corretto:

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

E sì: la generazione automatica AFAIK è possibile solo per i tipi numerici.

1

è necessario controllare "ejb3" o aggiungere nella configurazione:

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