成功捷径,贵在坚持
人为善,福虽未至祸已远离; 人为恶,祸虽未至福已远离

@SuppressWarnings("serial")
@Entity
@Table(name = "BOOK")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Book implements Serializable{

 private static final long serialVersionUID = -2700610405985954588L;
 
 private int oid;
 
 private String name;
 
 private String description;
 
 private Date publish;
 
 private Collection<Author> authors;

 @Id
 @GeneratedValue(strategy=GenerationType.SEQUENCE)
 public int getOid() {
  return oid;
 }

 public void setOid(int oid) {
  this.oid = oid;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getDescription() {
  return description;
 }

 public void setDescription(String description) {
  this.description = description;
 }

 @Temporal(value=TemporalType.TIMESTAMP)
 public Date getPublish() {
  return publish;
 }

 public void setPublish(Date publish) {
  this.publish = publish;
 }


 @ManyToMany(
   targetEntity=com.e104cn.pda.data.anno.Author.class,
   cascade ={CascadeType.PERSIST,CascadeType.MERGE},
            fetch=FetchType.LAZY
 )
 @JoinTable(
         name = "book_author",
         joinColumns ={@JoinColumn(name="book_id")},
         inverseJoinColumns ={@JoinColumn(name= "author_id")}
   )
 public Collection<Author> getAuthors() {
  return authors;
 }
 
 public void addAuthor(Author author){
  if(author == null){
   authors = new ArrayList<Author>();
  }
  if(!authors.contains(author)){
   authors.add(author);
  }
 }

 public void setAuthors(Collection<Author> authors) {
  this.authors = authors;
 }
 
}

/**@SuppressWarnings("serial")
@Entity
@Table(name = "AUTHOR")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)*/

public class Author implements Serializable{

 private static final long serialVersionUID = 7131973910486229579L;
 
 private int id;
 
 private String firstName;
 
 private String lastName;
 
 private boolean male;
 
 private Date birthday;
 
 @Transient
 private Collection<Book> books ;

 @Id
 @GeneratedValue(strategy=GenerationType.SEQUENCE)
 public int getId() {
  return id;
 }

 public void setId(int oid) {
  this.id = oid;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public boolean isMale() {
  return male;
 }

 public void setMale(boolean male) {
  this.male = male;
 }

 @Temporal(value=TemporalType.TIMESTAMP)
 public Date getBirthday() {
  return birthday;
 }

 public void setBirthday(Date birthday) {
  this.birthday = birthday;
 }

 @ManyToMany(mappedBy="authors",
  cascade={CascadeType.PERSIST,CascadeType.ALL}
 )
 public Collection<Book> getBooks() {
  return books;
 }

 public void addBook(Book book){
  if(books == null){
   books = new ArrayList<Book>();
  }
  if(!books.contains(book)){
   books.add(book);
  }
 }
 
 public void setBooks(Collection<Book> books) {
  this.books = books;
 }
 
}
1、去掉蓝色注释的部分即可解决错误.
2、 注:@ManyToMany(mappedBy="authors",属性一定要和book中的authors一致。

posted on 2008-05-20 08:54 选宝网an9 阅读(6514) 评论(0)  编辑  收藏 所属分类: Java Exception&Error

只有注册用户登录后才能发表评论。


网站导航: