2013年12月19日星期四

hibernate annotations one how to write

There are two tables A B
Table A fields:
id name bid ( foreign key )
Table B fields:
id name

entity class

@Eneity
public class A {

  @Id
  @GeneratedValue
  private int id;
  private Sreing name;
  
  @OneToOne       //这里该怎么写
  private int bid;

  //setter  getter......
}

@Eneity
public class B {

  @Id
  @GeneratedValue      //这里要写什么?
  private int id;
  private Sreing name;

  //setter  getter......
}


hql how to write in order to make final
select * from A
Return to the list so there
B 's name
------ Solution -------------------- ------------------------ entity class a landlord 's wrong


@ Eneity
public class A {

@ Id
@ GeneratedValue
private int id;
private Sreing name;

@ OneToOne / / Here how to write
private B b;

/ / setter getter ......
}
.
so check out the A, B 's name had a list.get (i) getB () getName ();.
------ Solution ----- ---------------------------------------
hibernate recommend using bidirectional many- to achieve one
------ Solution ------------------------------------ --------


@Entity
public class A {
//最好还是把这些注解写在getter上
  @Id
  @GeneratedValue
  private int id;
  private String name;
  private int bid;
  @OneToOne
  @JoinColumn(name="bid",insertable=false,updatable=false)
  private B b;

  //setter  getter......
}

@Eneity
public class B {
//最好还是把这些注解写在getter上
  @Id
  @GeneratedValue    
  private int id;
  private String name;
  @OneToOne(mappedBy="b")//双向一对一关联以A类的b属性配置来进行关联
  private A a;
  //setter  getter......
}


------ Solution -------------------------- ------------------
not hibernate annotations can look at this document http://docs.jboss.org/hibernate/annotations/3.4/reference/ zh_cn / html_single /
------ For reference only ---------------------------------- -----


is to B as it is an attribute of A
That comment how to write ?
------ For reference only ------------------------------------ ---


give a simple example you
------ For reference only --------------------------- ------------
you search on the Internet , many are
------ For reference only --------------- ------------------------


I could not find a satisfactory asked to come to this . . .
------ For reference only ---------------------------------------


Well I write mainly on getter on the property too lazy to play on my first try and see. .
------ For reference only -------------------------------------- -


OK solved. . . Thank you. .

没有评论:

发表评论