Hibernate @Embeddable Annotations

Source: Internet
Author: User

Implement a custom type in Hibernate, as long as the Usertype interface is implemented or provided as a component. JPA @embedded is a bit similar, with this comment you can use generic Java objects in your entity This object needs to be annotated with @embeddable

To give a simple example: the person class has a name attribute, name should have firstname,lastname two attributes, and the general notation writes two attributes directly in the entity:

Private String FirstName;

Private String LastName;

We can use a name class instead of this one, which contains FirstName and LastName, so that we write in the entity as follows:

private name name;

That's it. What about the name class, then?

import java.io.Serializable;

Import javax.persistence.Embeddable;

@Embeddable

Public class Name implements Serializable {

Private String FirstName;

Private String LastName;

public Name () {

}

Public Name (String firstName, String lastName) {

This.firstname = FirstName;

This.lastname = LastName;

}

Public String Getfirstname () {

return firstName;

}

public void Setfirstname (String firstName) {

This.firstname = FirstName;

}

Public String Getlastname () {

return lastName;

}

public void Setlastname (String lastName) {

This.lastname = LastName;

}

@Override

Public String toString () {

Return firstname+ "" +lastname;

}

}

The value of the note is:

1. The serializable interface must be implemented

2. Need to have an argument-free constructor

[email protected] Note that this class can be inserted into an entity

It's not finished! The name attribute in the person class needs to be mapped to the First,last two fields in the database table, as follows:

@Embedded

@AttributeOverrides ({

@AttributeOverride (name = "FirstName", column = @Column (name = "First_Name"),

@AttributeOverride (name = "LastName", column = @Column (name = "Last_Name")})

Public Name GetName () {

return name;

}

The firstname,lastname of the name class is mapped with the first_name,last_name of the table in the database by @attributeoverride annotations.

It's simple, it looks pretty cool. Can find a small-sized disadvantage, for example, I would like to query a name Dennis Zane, if it is hibernate, I may do this:

Session.createquery ("from person p where p.name=?"). Setparameter (0,name). List ();

Hibernate will automatically match your custom type, but if I write this in JPA:

Em.createquery ("Select p from person p where p.name=:name"). Setparameter ("name", name);

Query error ... Depressed, the entire name object as a query parameter passed in error, I have to write:

Em.createquery ("Select p from person p where p.name.firstname=:name1 and P.name.lastname=:name2"). Setparameter ("Name1" , Name.getfirstname ()). Setparameter ("name2", name.getlastname);

That is, you need to map each property of name.

Hibernate @Embeddable Annotations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.