3

I'm using Hibernate to create entities class from MS SQL. But with NVARCHAR Hibernate changes to Serializable type. It makes some errors, exeptions because conflict between NVARCHAR and Serializable when I CRUD!

Have anyway to change Serializable to String when Hibernate auto creates entities class?

Sorry, my Eng skills aren't good so I can't find information of this problem in this site.

I'm using JBoss tool for LUNA Eclipse and I do following steps:

enter image description here

enter image description here

This is a class after create by Hibernate

@Entity
@Table(name = "SubMenu1", catalog = "MySite")
public class SubMenu1 implements java.io.Serializable {

private byte id;
private Menu menu;
private Serializable name;

public SubMenu1() {
}

public SubMenu1(byte id, Menu menu) {
    this.id = id;
    this.menu = menu;
}

public SubMenu1(byte id, Menu menu, Serializable name) {
    this.id = id;
    this.menu = menu;
    this.name = name;
}

@Id
@Column(name = "id", unique = true, nullable = false)
@GeneratedValue(strategy=GenerationType.AUTO)
public byte getId() {
    return this.id;
}

public void setId(byte id) {
    this.id = id;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "idMenuParent", nullable = false)
public Menu getMenu() {
    return this.menu;
}

public void setMenu(Menu menu) {
    this.menu = menu;
}

@Column(name = "name")
public Serializable getName() {
    return this.name;
}

public void setName(Serializable name) {
    this.name = name;
}

I want Serializable changes to String that do automaticly, i don't need change by my hands

  • possible duplicate of [Mapping to varchar and nvarchar in hibernate](http://stackoverflow.com/questions/1306774/mapping-to-varchar-and-nvarchar-in-hibernate) – steve cook May 06 '15 at 09:17
  • Maybe I can't make my problem clear so I explain with a little information, pls help – Đào Duy Tùng May 06 '15 at 09:29
  • Try adding `columnDefinition` attribute. – OO7 May 06 '15 at 09:30
  • 1
    Edit ur `hibernate.reveng.xml` file to include this entry `` given in ``. Check whether Hibernate Code Generation wizard generates the filed of type `String` where the `NVARCHAR` datatype finds. – OO7 May 06 '15 at 09:41
  • I have added this as answer to ur question so that others also get benefit of this. You should accept it to indicate it's a right answer to this. – OO7 May 06 '15 at 11:55

1 Answers1

5

Edit your hibernate.reveng.xml file & add the following entry in the <type-mapping> section of the file:-

<sql-type jdbc-type="NVARCHAR" hibernate-type="string"/>

When the Hibernate Generation Wizard will generate the classes for you then it uses hibernate.reveng.xml for type conversion. So when the generator encounter NVARCHAR datatype it check for it's equivalent hibernate data type & convert it into String type.

OO7
  • 2,785
  • 1
  • 21
  • 33