Hibernate annotation-entity Annotation

Source: Internet
Author: User

Hibernate annotation-entity Annotation
@ Entity @ Table (name = "EMP", schema = "SCOTT") public class Emp implements java. io. serializable {// Fields private Short empno; private Dept dept; private String ename; private String job; private Short mgr; private Date hiredate; private Double sal; private Double comm; // Constructors/** default constructor */public Emp () {}/ ** full constructor */public Emp (Dept dept, String ename, String job, Short mgr, date hiredate, Double sal, Double comm) {this. dept = dept; this. ename = ename; this. job = job; this. mgr = mgr; this. hiredate = hiredate; this. sal = sal; this. comm = comm;} // Property accessors @ Id @ GeneratedValue @ Column (name = "EMPNO", unique = true, nullable = false, precision = 4, scale = 0) public Short getEmpno () {return this. empno;} public void setEmpno (Short empno) {this. empno = empno;} @ ManyToOne (fetch = FetchType. LAZY) @ JoinColumn (name = "DEPTNO") public Dept getDept () {return this. dept;} public void setDept (Dept dept) {this. dept = dept;} @ Column (name = "ENAME", length = 10) public String getEname () {return this. ename;} public void setEname (String ename) {this. ename = ename;} @ Column (name = "JOB", length = 9) public String getJob () {return this. job;} public void setJob (String job) {this. job = job;} @ Column (name = "MGR", precision = 4, scale = 0) public Short getMgr () {return this. mgr;} public void setMgr (Short mgr) {this. mgr = mgr;} @ Temporal (TemporalType. DATE) @ Column (name = "HIREDATE", length = 7) public Date getHiredate () {return this. hiredate;} public void setHiredate (Date hiredate) {this. hiredate = hiredate;} @ Column (name = "SAL", precision = 7) public Double getSal () {return this. sal;} public void setSal (Double sal) {this. sal = sal ;}@ Column (name = "COMM", precision = 7) public Double getComm () {return this. comm;} public void setComm (Double comm) {this. comm = comm ;}} the two annotation @ Entity above the class indicates that the name attribute value in the object @ Table is the database Table name, And the schema attribute value is the database user name. The default value is "", you can leave it empty. Generally, writing these two attributes is enough. others are not introduced. attribute annotation is best written at the top of the get method: 1. @ id indicates that this is the OID attribute and corresponds to the primary key of the database. Note that the OID must be long, int, or mongo.2. @ GenerateValue has two attributes. strategy indicates that the primary key generation policy, and the default value is StrategyType. AUTO (the other three values are: IDENTITY, SEQUENCE, TABLE). AUTO indicates that hibernate automatically selects IDENTITY to generate an IDENTITY based on the underlying database. It supports mysql, db2, sqlserver, sybase and other database SEQUENCE are also generated by the underlying database. oracle, db2, and PostgreSQL table are supported, indicating that the SEQUENCE is generated based on another table. @ Column: You can configure the ing between the object attribute and the database table field. The attribute value is the table field name unique. Is the unique nullable? Can it be the nullprecision effective bits? scale length Data length insertable? Can this field be inserted? whether the updateable field in the database table can update columnDefinition to set the Data Type of the corresponding table field, not commonly used 4. @ Temporal it has only one value attribute, so this value can be omitted. Its value is the enumerated value TemporalTypeTemporalType. DATE is accurate to year, month, and day, for example, "2015-11-04" TemporalType. the TIME is accurate to the hour, minute, and second, for example, "20:00:00" TemporalType. TIMESTAMP date time is accurate to year, month, day, hour, minute, and second, for example, "20:00:00" @ BLob @ Clob5. @ Version mark 6. @ Transient indicates that this attribute is free and will not be persistent in the database. 6. @ ManyToOne first occupies the trap and writes it in detail next time. @ JoinColumn8. @ OneToMany9. @ ManyToMany10. @ OneToOne: configure the Union primary key. Here we select an implementation method. To tell the truth, it is really troublesome to configure the Union primary key of hibernate. this is the first class automatically generated by myeclipse @ Embeddablepublic class BonusId implements java. io. serializable {private String ename; private String job; private BigDecimal sal; private BigDecimal comm ;...} the second class @ Entity @ Table (name = "BONUS", schema = "SCOTT") public class Bonus implements java. io. serializable {private BonusId id; @ EmbeddedId @ AttributeOverrides ({@ AttributeOverride (name = "ename", column = @ Column (name = "ENAME", length = 10 )), @ AttributeOverride (name = "job", column = @ Column (name = "JOB", length = 9), @ AttributeOverride (name = "sal ", column = @ Column (name = "SAL", precision = 22, scale = 0), @ AttributeOverride (name = "comm ", column = @ Column (name = "COMM", precision = 22, scale = 0)}) public BonusId getId () {return this. id;} public void setId (BonusId id) {this. id = id ;}} Sequence primary key generation policy. This is a bit complicated. First, we have created the Sequence seq_studentclass class as @ Entity @ SequenceGenerator (name = "seq_t_student" sequenceName = 'seq _ student ') @ Table (name = "student" schma = "" public class Student {private long id; private void setId (long id) {this. id = id;} @ Id @ GeneratedValue (strategy = Generation. SEQUENCE, generator = "seq_t_student") @ Column (name = "ID", length = 40, nullable = false, unique = true) public long getId () {return this. id }}

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.