Hibernate-one-to-multiple unidirectional Association ing and hibernate ing

Source: Internet
Author: User

Hibernate-one-to-multiple unidirectional Association ing and hibernate ing


[Hibernate]-about one-to-multiple unidirectional Association ing


One-to-Multiple Association ing based on foreign keys! One-to-multiple, Group for Person, a Group can have multiple people! OK?

Hibernate mainly has two Configuration Methods: Annotations and XML!


The following describes how to configure Annotations!

@ Entity @ Table (name = "t_group") publicclass Group {private Integer id; private String name; private Set <Person> persons = newHashSet <Person> (); // set is not allowed to be repeated. It is best for database model @ Id @ GeneratedValue public Integer getId () {returnid;} publicvoid setId (Integerid) {this. id = id ;}@ Column (name = "t_name") public String getName () {returnname;} publicvoid setName (Stringname) {this. name = name;} @ onetoworkflow // Add @ JoinColumn to prevent Hibernate from processing one-to-multiple operations as multiple-to-many operations and generate an intermediate Table @ JoinColumn (name = "group_id ") public Set <Person> getPersons () {returnpersons;} publicvoidsetPersons (Set <Person> persons) {this. persons = persons ;}}

@Entity@Table(name="t_person")publicclass Person {    private Integer id;    private String name;    private Integer age;    @Id    @GeneratedValue    public Integer getId() {       returnid;    }    publicvoid setId(Integerid) {       this.id = id;    }    @Column(name="t_name")    public String getName() {       returnname;    }    publicvoid setName(Stringname) {       this.name = name;    }    @Column(name="t_age")    public Integer getAge() {       returnage;    }    publicvoid setAge(Integerage) {       this.age = age;    }}

Configure one-to-multiple one-way Association ing in XML
publicclass Person {    private Integer id;    private String name;    private Integer age;    public Integer getId() {       returnid;    }    publicvoid setId(Integerid) {       this.id = id;    }    public String getName() {       returnname;    }    publicvoid setName(Stringname) {       this.name = name;    }    public Integer getAge() {       returnage;    }    publicvoid setAge(Integerage) {       this.age = age;    }}

Publicclass Group {private Integer id; private String name; private Set <Person> persons = newHashSet <Person> (); // The set cannot be repeated, the most suitable database model is public Integer getId () {returnid;} publicvoid setId (Integerid) {this. id = id;} public String getName () {returnname;} publicvoid setName (Stringname) {this. name = name;} public Set <Person> getPersons () {returnpersons;} publicvoidsetPersons (Set <Person> persons) {this. persons = persons ;}}

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">


OK, one-to-multiple one-way Association ing Annotations and XML configuration methods!




What is the difference between one-to-multiple mappings between one-way associations and two-way mappings in hibernate?

<One-to-one association> one-way Association is to maintain the relationship only unilaterally. Two-way Association is to maintain the relationship on both sides, and to determine whether one-way Association is used or two-way Association, it depends on your business requirements. If one-way contact can meet the business requirements you need, it must be one-way Association. (My suggestion is to use one-way Association, using Bidirectional Association makes the relationship complex and prone to problems ). The Association also involves the issue of control. in <one-to-many>, it is best to give control to the other party (who is responsible for maintaining the relationship between them ),

How does one understand the bidirectional one-to-Multiple Association and unidirectional one-to-Multiple Association of hibernate?

Hibernate

Currently, Hibernate is very popular. It is a Java O/R ing framework for source code development. It aims to free developers from tedious data persistence programming. It also has a standard HQL (Hibernate query language) language, which you can see in the new ejb ql. Hibernate is very simple in terms of data query, update, connection pool, transaction processing, and entity link processing.

Overview

The submitted EJB3.0 specification involves two major changes:

1. A set of annotation-based EJB programming models, coupled with the application behavior defined in EJB2.1 through the deployment descriptor and several interfaces.
2. The new object Bean persistence model and EJBQL also have many important changes.

There are also some proposals, such as a new client programming model, the use of business interfaces, and the lifecycle of entity beans. Note that the EJB2.1 programming model (including the deployment descriptor and home/remote interface) is still valid. The new simplified model does not completely replace EJB2.1.

EJB comment

An important goal of the EJB specification organization is to reduce the number of original code, and they provide a perfect way to introduce it. In EJB3.0, any type of enterprise-level Bean is a simple Java object (POJO) that has been properly annotated ). Annotations can be used to define the business interface, O/R ing information, and resource reference information of beans. The effect is the same as that of the deployment descriptor and interface defined in EJB2.1. Deploy descriptors in EJB3.0 is no longer necessary; the home interface is no longer required, and you do not need to implement business interfaces (containers can do these tasks for you ).

For example, you can use the @ Stateless annotation tag class to declare a Java class as a Stateless Session bean. For stateful session beans, the @ Remove annotation can be used to mark a specific method. Through this annotation, the bean instance will be cleared after the method is called.

To reduce the description of the component, the organization also adopted the configuration-by-exception method, this means that you can provide a clear default value for all comments so that most common information can be inferred accordingly.

New persistence Model

The new entity bean is also a simple Java object (POJO) with annotations added ). Once accessed by EntityManager, it becomes a persistent object and a part of the persistent context. A persistent context is loosely coupled with a transaction context. Strictly speaking, it implies coexistence with a transaction session.

Entity relationships are also defined through annotations, and O/R ing is also required, and several different database standard operations are provided, in EJB2.1, this is done through the developer's own design patterns or other technologies (for example, self-increasing primary key policies ).

In-depth research

Now it is time to learn more about the EJB3.0 draft. Let's start to explore four enterprise-level beans in all ejbs and see what they look like in the new specifications.

Stateless Session bean

In the EJB3.0 specification, writing a Stateless Session bean (SLSB) only requires a simple Java file and adds @ Stateless annotation to the class layer. This bean can extend the javax. ejb. SessionBean interface, but these are not necessary.

An SLSB no longer needs the home interface, and no EJB needs it. Bean class can implement business interfaces or not. If no business interface is implemented, the business interface is generated by any public method. If only a few business methods are exposed to the business interface, you can use the @ BusinessMethod annotation for these methods. By default, all production interfaces are local interfaces. You can also use the @ Remote annotation to declare this interface as a remote interface.

The following rows... the remaining full text>

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.