2014-12-09 4 views
0

나는 Hibernate ORM과 Spring Framework를 사용하는 Java 프로젝트를 작성 중이다.어노테이션 기반 Hibernate 매핑을 만드는 방법은 무엇입니까?

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
    <session-factory> 
     <mapping class="somepackage.class1"/> 
     <mapping class="somepackage.class2"/> 
     <!-- etc. --> 
    </session-factory> 
</hibernate-configuration> 

그럼, 내가 주석 기반의 클래스를 만들 : 나는 POJO 클래스를 추가 할 때 지금, 나는 다음과 같습니다 내 hibernate.cfg.xml 파일을 수정해야합니다. 적절한 Hibernate 어노테이션을 사용했다면 hibernate.cfg.xml에 클래스 별 매핑을 추가하는 것을 피할 수 있다고 들었다. XML 파일의 매핑을 피하기 위해 클래스를 수정하려면 어떻게해야합니까? 난 당신이 때문에 봄이 제공하는 유연성 스프링 구성으로 최대 절전 모드 설정을 내보내 게 좋을 것

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 

package somepackage.pojo; 

import java.io.Serializable; 
import java.util.Collection; 
import javax.persistence.Basic; 
import javax.persistence.CascadeType; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.Id; 
import javax.persistence.NamedQueries; 
import javax.persistence.NamedQuery; 
import javax.persistence.OneToMany; 
import javax.persistence.Table; 
import javax.validation.constraints.NotNull; 
import javax.validation.constraints.Size; 
import javax.xml.bind.annotation.XmlRootElement; 
import javax.xml.bind.annotation.XmlTransient; 

/** 
* 
* @author D 
*/ 
@Entity 
@Table(name = "ACCOUNT") 
@XmlRootElement 
@NamedQueries({ 
    @NamedQuery(name = "Account.findAll", query = "SELECT a FROM Account a"), 
    @NamedQuery(name = "Account.findByLogin", query = "SELECT a FROM Account a WHERE a.login = :login"), 
    @NamedQuery(name = "Account.findByPassword", query = "SELECT a FROM Account a WHERE a.password = :password")}) 
public class Account implements Serializable { 
    private static final long serialVersionUID = 1L; 
    @Id 
    @Basic(optional = false) 
    @NotNull 
    @Size(min = 1, max = 100) 
    @Column(name = "LOGIN", nullable = false, length = 100) 
    private String login; 
    @Size(max = 128) 
    @Column(name = "PASSWORD", length = 128) 
    private String password; 
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "author") 
    private Collection<Comment> commentCollection; 
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "author") 
    private Collection<Article> articleCollection; 

    public Account() { 
    } 

    public Account(String login) { 
     this.login = login; 
    } 

    public String getLogin() { 
     return login; 
    } 

    public void setLogin(String login) { 
     this.login = login; 
    } 

    public String getPassword() { 
     return password; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 

    @XmlTransient 
    public Collection<Comment> getCommentCollection() { 
     return commentCollection; 
    } 

    public void setCommentCollection(Collection<Comment> commentCollection) { 
     this.commentCollection = commentCollection; 
    } 

    @XmlTransient 
    public Collection<Article> getArticleCollection() { 
     return articleCollection; 
    } 

    public void setArticleCollection(Collection<Article> articleCollection) { 
     this.articleCollection = articleCollection; 
    } 

    @Override 
    public int hashCode() { 
     int hash = 0; 
     hash += (login != null ? login.hashCode() : 0); 
     return hash; 
    } 

    @Override 
    public boolean equals(Object object) { 
     // TODO: Warning - this method won't work in the case the id fields are not set 
     if (!(object instanceof Account)) { 
      return false; 
     } 
     Account other = (Account) object; 
     if ((this.login == null && other.login != null) || (this.login != null && !this.login.equals(other.login))) { 
      return false; 
     } 
     return true; 
    } 

    @Override 
    public String toString() { 
     return "somepackage.pojo.Account[ login=" + login + " ]"; 
    } 

} 

답변

1

: 여기 넷빈즈에 의해 생성 된 내 예를 들어, POJO 파일이다. 새 엔티티를 만들 때마다 구성에서 클래스를 선언하지 않아도됩니다. 스프링 구성을 사용하여 다음을 수행 할 수 있습니다. (packagestoscan 속성)

<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
      <property name="dataSource" ref="dataSource" /> 
      <property name="packagesToScan" value="org.baeldung.spring.persistence.model" /> 
      <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> 
       <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
      </props> 
      </property> 
     </bean> 

참조 : http://www.javacodegeeks.com/2013/05/hibernate-3-with-spring.html

관련 문제