Created
July 23, 2012 03:14
-
-
Save fankay/3161856 to your computer and use it in GitHub Desktop.
Hibernate Annotation 一对一配置
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity | |
@Table(name="t_card") | |
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE) | |
public class Card { | |
private int id; | |
private String cardnum; | |
private User user; | |
@Id | |
@GeneratedValue(generator="myPK") | |
@GenericGenerator(name="myPK",strategy="foreign",parameters={@Parameter(name="property",value="user")}) | |
public int getId() { | |
return id; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
public String getCardnum() { | |
return cardnum; | |
} | |
public void setCardnum(String cardnum) { | |
this.cardnum = cardnum; | |
} | |
@OneToOne(mappedBy="card") | |
public User getUser() { | |
return user; | |
} | |
public void setUser(User user) { | |
this.user = user; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
User user = new User(); | |
user.setUsername("uu"); | |
user.setPassword("uuuu"); | |
Card card = new Card(); | |
card.setCardnum("x09876"); | |
user.setCard(card); | |
card.setUser(user); | |
session.save(user); | |
session.save(card); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity | |
@Table(name="t_user") | |
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE) | |
public class User { | |
private int id; | |
private String username; | |
private String password; | |
private List<Address> addressList; | |
private Card card; | |
@Id | |
@GeneratedValue(strategy=GenerationType.IDENTITY) | |
public int getId() { | |
return id; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
@Column(name="username") | |
public String getUsername() { | |
return username; | |
} | |
@Transient | |
public String getMessage() { | |
return "xixi"; | |
} | |
public void setUsername(String username) { | |
this.username = username; | |
} | |
public String getPassword() { | |
return password; | |
} | |
public void setPassword(String password) { | |
this.password = password; | |
} | |
@OneToMany(mappedBy="user") | |
@OrderBy("id desc") | |
public List<Address> getAddressList() { | |
return addressList; | |
} | |
public void setAddressList(List<Address> addressList) { | |
this.addressList = addressList; | |
} | |
@OneToOne | |
@PrimaryKeyJoinColumn | |
public Card getCard() { | |
return card; | |
} | |
public void setCard(Card card) { | |
this.card = card; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment