`
lbfhappy
  • 浏览: 81902 次
社区版块
存档分类
最新评论

J2EE初学2

阅读更多

今天看到了实体BEAN,(Entity Bean),在做对象和数据库表映射时候,发现一个问题,那就是我为一个字段指定了列名后,它仍然会生成一个和字段名一样的列,比如;

实体BEAN代码
  1. /*  
  2.  * Person.java  
  3.  *  
  4.  * Created on 2006年12月8日, 上午9:43  
  5.  *  
  6.  * To change this template, choose Tools | Template Manager  
  7.  * and open the template in the editor.  
  8.  */  
  9.   
  10. package lbf.demo.bean;   
  11.   
  12. /**  
  13.  *  
  14.  * @author lbf  
  15.  */  
  16. import java.util.*;   
  17. import javax.persistence.*;   
  18. @Entity  
  19. @Table(name="person")   
  20. public class Person {   
  21.     private int id,age;   
  22.     private String name,sex;   
  23.     private Date birthday;   
  24.     /** Creates a new instance of Person */  
  25.     public Person() {   
  26.     }   
  27.     @Id  
  28.     @GeneratedValue  
  29.     public int getId() {   
  30.         return id;   
  31.     }   
  32.   
  33.     public void setId(int id) {   
  34.         this.id = id;   
  35.     }   
  36.     @Column(name="myAge",nullable=false,length=4)   
  37.     public int getAge() {   
  38.         return age;   
  39.     }   
  40.   
  41.     public void setAge(int age) {   
  42.         this.age = age;   
  43.     }   
  44.     @Column(name="PersonName",nullable=false,length=32)   
  45.     public String getName() {   
  46.         return name;   
  47.     }   
  48.        
  49.     public void setName(String name) {   
  50.         this.name = name;   
  51.     }   
  52.     @Column(name="mySex",nullable=false)   
  53.     public String getSex() {   
  54.         return sex;   
  55.     }   
  56.   
  57.     public void setSex(String sex) {   
  58.         this.sex = sex;   
  59.     }   
  60.     @Column(name="myBirty",nullable=false)   
  61.     public Date getBirthday() {   
  62.         return birthday;   
  63.     }   
  64.   
  65.     public void setBirthday(Date birthday) {   
  66.         this.birthday = birthday;   
  67.     }   
  68.        
  69. }   

我在这里,除了主键ID没有指定列名外,其它四个属性我都有指定列名,当然这个列名和属性本不一样,可是当我布署了此EJB后,发现数据库里面我生成的表却多了几列和类属性同名的列.真是太奇怪了,它既生成我指定的列名为什么又要生成属性相同的列名呢,如果我不指定任何列名,那么数据库就会生成和我属性名一样的列.

上述生成的重复列,只是有三个会重复而已,而PersonName却只有一个,也就是说不会出现name列,但是为什么其它三个会生成age,birthday,sex这三列呢.此时它依然帮我生成了myAge,myBirth,mySex,并且这三列还可以为空,我不是指定了不可以为空的吗,可是age,birthday,sex,这三列却不可以为空,就好像我指定的属性是为它们指定的一样.

记下此问题,留给以后解决

刚才又改了一个程序,发现这样写可以让数据库生成我想生成的形式

新的代码 代码
  1. /*  
  2.  * Person.java  
  3.  *  
  4.  * Created on 2006年12月8日, 上午9:43  
  5.  *  
  6.  * To change this template, choose Tools | Template Manager  
  7.  * and open the template in the editor.  
  8.  */  
  9.   
  10. package lbf.demo.bean;   
  11.   
  12. /**  
  13.  *  
  14.  * @author lbf  
  15.  */  
  16. import java.util.*;   
  17. import javax.persistence.*;   
  18. @Entity  
  19. @Table(name="person")   
  20. public class Person {   
  21.     private int id,age;   
  22.     private String name,sex;   
  23.     private Date birthday;   
  24.     /** Creates a new instance of Person */  
  25.     public Person() {   
  26.     }   
  27.     @Id  
  28.     @GeneratedValue  
  29.     public int getId() {   
  30.         return id;   
  31.     }   
  32.   
  33.     public void setId(int id) {   
  34.         this.id = id;   
  35.     }   
  36.     @Column(name="myAge",nullable=false,length=4)   
  37.     public int getAge() {   
  38.         return age;   
  39.     }   
  40.   
  41.     public void setAge(int age) {   
  42.         this.age = age;   
  43.     }   
  44.     @Column(name="PersonName",nullable=false,length=32)   
  45.     public String getName() {   
  46.         return name;   
  47.     }   
  48.        
  49.     public void setName(String name) {   
  50.         this.name = name;   
  51.     }   
  52.     @Column(name="mySex",nullable=false,length=32)   
  53.     public String getSex() {   
  54.         return sex;   
  55.     }   
  56.   
  57.     public void setSex(String sex) {   
  58.         this.sex = sex;   
  59.     }   
  60.     @Column(name="myBirty",nullable=false,length=8)   
  61.     public Date getBirthday() {   
  62.         return birthday;   
  63.     }   
  64.   
  65.     public void setBirthday(Date birthday) {   
  66.         this.birthday = birthday;   
  67.     }   
  68.        
  69. }   

在这段代码里,我每次声明一样新的列名的时候都指定了是否可以非空,长度为多少,这我们在新建表的时候也必须指出的东西,所以我觉得以前的代码之所以会出错(其实不能叫出错,叫出乎意料)是因为我只是指定了列名,而没有指定其长度或者约束,也就是说我的定义不完全,所以系统不知道我想建什么样的列,于是就帮我建了两列,一列是我命名的,一例是以属性名命名的,呵呵,现在只能这么理解了.因为才刚刚接触到皮毛而已

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics