简体   繁体   中英

Mapping Hibernate to MySQL (BEGINNER)

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 (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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM