当前位置: 代码迷 >> J2EE >> 关于hibernate的annotation跟配置文件
  详细解决方案

关于hibernate的annotation跟配置文件

热度:96   发布时间:2016-04-21 21:37:30.0
关于hibernate的annotation和配置文件
在看视频学hibernate,不知道最近企业用这两个方面哪个多一点,视频里说,主要用annotation,想请教请教大家
hibernate annotation

------解决方案--------------------

<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>

<property name="annotatedClasses">
<list>
<value>com.core.mvc.po.Student</value>
<value>com.core.mvc.po.Teacher</value>
<value>com.core.mvc.po.Comment</value>
<value>com.core.mvc.po.Department</value>
<value>com.core.mvc.po.Employee</value>
                                   把要用大实体类写在这里
</list>
</property>
</bean>

------解决方案--------------------
以下是通过annotation注解的javabean,参考一下:

package db.hbm;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import org.hibernate.annotations.GenericGenerator;

/**
 * Users entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "USERS", uniqueConstraints = @UniqueConstraint(columnNames = "USERNAME"))
public class Users implements java.io.Serializable {

// Fields

private String id;
private String username;
private String fullname;
private String password;
private String depid;
private String locked;
private String changepassword;
private String dutyid;
private String mobile;
private String sex;
private String phonework;
private String phonehome;
private String email;
private String entrust;
private String style;
private String keynum;
private String keypas;
private String backup1;
private String backup2;
private String backup3;
private String backup4;
private String backup5;

// Constructors

/** default constructor */
public Users() {
}

/** minimal constructor */
public Users(String username, String fullname, String password,
String depid, String locked, String changepassword) {
this.username = username;
this.fullname = fullname;
this.password = password;
this.depid = depid;
this.locked = locked;
this.changepassword = changepassword;
}

/** full constructor */
public Users(String username, String fullname, String password,
String depid, String locked, String changepassword, String dutyid,
String mobile, String sex, String phonework, String phonehome,
String email, String entrust, String style, String keynum,
String keypas, String backup1, String backup2, String backup3,
String backup4, String backup5) {
this.username = username;
this.fullname = fullname;
this.password = password;
this.depid = depid;
this.locked = locked;
this.changepassword = changepassword;
this.dutyid = dutyid;
this.mobile = mobile;
this.sex = sex;
this.phonework = phonework;
this.phonehome = phonehome;
this.email = email;
this.entrust = entrust;
this.style = style;
this.keynum=keynum;
this.keypas=keypas;
this.backup1=backup1;
this.backup2=backup2;
this.backup3=backup3;
this.backup4=backup4;
this.backup5=backup5;
}

// Property accessors
@GenericGenerator(name = "generator", strategy = "uuid")
@Id
@GeneratedValue(generator = "generator")
@Column(name = "ID", unique = true, nullable = false, length = 50)
  相关解决方案