在看视频学hibernate,不知道最近企业用这两个方面哪个多一点,视频里说,主要用annotation,想请教请教大家hibernateannotation

解决方案 »

  1.   


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

  2.   

    以下是通过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)
    public String getId() {
    return this.id;
    } public void setId(String id) {
    this.id = id;
    } @Column(name = "USERNAME", unique = true, nullable = false, length = 20)
    public String getUsername() {
    return this.username;
    } public void setUsername(String username) {
    this.username = username;
    } @Column(name = "FULLNAME", nullable = false, length = 20)
    public String getFullname() {
    return this.fullname;
    } public void setFullname(String fullname) {
    this.fullname = fullname;
    } @Column(name = "PASSWORD", nullable = false, length = 20)
    public String getPassword() {
    return this.password;
    } public void setPassword(String password) {
    this.password = password;
    } @Column(name = "DEPID", nullable = false, length = 50)
    public String getDepid() {
    return this.depid;
    } public void setDepid(String depid) {
    this.depid = depid;
    } @Column(name = "LOCKED", nullable = false, length = 1)
    public String getLocked() {
    return this.locked;
    } public void setLocked(String locked) {
    this.locked = locked;
    } @Column(name = "CHANGEPASSWORD", nullable = false, length = 1)
    public String getChangepassword() {
    return this.changepassword;
    } public void setChangepassword(String changepassword) {
    this.changepassword = changepassword;
    } @Column(name = "DUTYID", length = 50)
    public String getDutyid() {
    return this.dutyid;
    } public void setDutyid(String dutyid) {
    this.dutyid = dutyid;
    } @Column(name = "MOBILE", length = 20)
    public String getMobile() {
    return this.mobile;
    } public void setMobile(String mobile) {
    this.mobile = mobile;
    } @Column(name = "SEX", length = 2)
    public String getSex() {
    return this.sex;
    } public void setSex(String sex) {
    this.sex = sex;
    } @Column(name = "PHONEWORK", length = 20)
    public String getPhonework() {
    return this.phonework;
    } public void setPhonework(String phonework) {
    this.phonework = phonework;
    } @Column(name = "PHONEHOME", length = 20)
    public String getPhonehome() {
    return this.phonehome;
    } public void setPhonehome(String phonehome) {
    this.phonehome = phonehome;
    } @Column(name = "EMAIL", length = 100)
    public String getEmail() {
    return this.email;
    } public void setEmail(String email) {
    this.email = email;
    } @Column(name = "ENTRUST", length = 50)
    public String getEntrust() {
    return this.entrust;
    } public void setEntrust(String entrust) {
    this.entrust = entrust;
    } @Column(name = "STYLE", length = 50)
    public String getStyle() {
    return style;
    } public void setStyle(String style) {
    this.style = style;
    } @Column(name = "KEYNUM", length = 50)
    public String getKeynum() {
    return keynum;
    } public void setKeynum(String keynum) {
    this.keynum = keynum;
    } @Column(name = "KEYPAS", length = 50)
    public String getKeypas() {
    return keypas;
    } public void setKeypas(String keypas) {
    this.keypas = keypas;
    } @Column(name = "BACKUP1", length = 100)
    public String getBackup1() {
    return backup1;
    } public void setBackup1(String backup1) {
    this.backup1 = backup1;
    } @Column(name = "BACKUP2", length = 100)
    public String getBackup2() {
    return backup2;
    } public void setBackup2(String backup2) {
    this.backup2 = backup2;
    } @Column(name = "BACKUP3", length = 100)
    public String getBackup3() {
    return backup3;
    } public void setBackup3(String backup3) {
    this.backup3 = backup3;
    } @Column(name = "BACKUP4", length = 100)
    public String getBackup4() {
    return backup4;
    } public void setBackup4(String backup4) {
    this.backup4 = backup4;
    } @Column(name = "BACKUP5", length = 100)
    public String getBackup5() {
    return backup5;
    } public void setBackup5(String backup5) {
    this.backup5 = backup5;
    }}