开发者

Mapping Hibernate to MySQL (BEGINNER)

开发者 https://www.devze.com 2023-04-03 17:53 出处:网络
Is there a way to map an Entity class (Address.java) to MySQL without using Hibernate Mapping (.hbm files)

Is there a way to map an Entity class (Address.java) to MySQL without using Hibernate Mapping (.hbm files)

What i need to do is to Create a table called 'Address' in the MySQL DB, and create Columns with the attributes of this class, and later populate them.

But i need to do it without writing Hibernate mapping files (开发者_C百科The .hbm files).

Persistence is one way of doing it, If so can someone tell me how to do it ?


Every ORM mapping has three parts:

  1. Object(s)
  2. Relational table(s)
  3. Mapping(s)

You have to have two to generate the third. If you have an object and its mapping, you can generate the table. If you have the table and the mapping, you can generate the object.

The only way you can generate without having to establish mappings is to use a framework like Grails that favors convention over configuration. That means they make assumptions about the mapping on your behalf.

But the ORM tool can't read your mind.


Use annotations instead of XML: http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#entity-overview

In your case, you would start by annotating the Address class with @Entity:

@Entity
@Table(name = "Address")
public class Address implements Serializable
{
    // fields
    // ctors
    // getters & setters
}
0

精彩评论

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