How to change Serializable to String when using Hibernate

I am using Hibernate to create an object class from MS SQL. But with NVARCHAR Hibernate the Serializable type changes. This throws some errors, exceptions due to conflict between NVARCHAR and Serializable when I CRUD!

Is there anyway to change Serializable to String when Hibernate automatically creates the object class?

Sorry, my Eng skills are not very good, so I cannot find information on this issue on this site.

I am using JBoss tool for LUNA Eclipse and I am doing the following steps:

enter image description here

enter image description here

This is the class after being created with 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 to be done automatically, I don't need manual changes.

+3


source to share


1 answer


Modify the file hibernate.reveng.xml

and add the following entry to the <type-mapping>

file section : -

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

      



When the Hibernate Generation wizard generates the classes for you, it uses it hibernate.reveng.xml

to convert the types. So when a generator encounters a data type NVARCHAR

, check its equivalent hibernate data type

and convert it to a type String

.

+4


source







All Articles