2

我知道有很多关于它的问题,但我找不到适合我的问题的答案。

我使用 Jboss 作为 7,Spring 和 Hibernate (4) 作为 JPA 2.0 提供者,所以我有简单的@OneToMany 双向关系:

我有这样的超级类人:

@MappedSuperclass
@Inheritance(strategy=InheritanceType.JOINED)
public abstract class Person {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;

@NotNull
@Size(min = 1, max = 25)
@Pattern(regexp = "[A-Za-z ]*", message = "must contain only letters and spaces")
private String name;  


public Person(String name) {
    super();
    this.name = name;
}

和班级成员:

@Entity
@Table(uniqueConstraints = @UniqueConstraint(columnNames = "email"))
public class Member extends Person implements Serializable
{
   /** Default value included to remove warning. Remove or modify at will. **/
   private static final long serialVersionUID = 1L;


   @NotNull
   @NotEmpty
   @Email
   private String email;

   @NotNull
   @Size(min = 10, max = 12)
   @Digits(fraction = 0, integer = 12)
   @Column(name = "phone_number")
   private String phoneNumber;

   @OneToMany(cascade=CascadeType.ALL , mappedBy="member" , fetch=FetchType.EAGER)
   private List<Order> orders;

还有班级订单:

@Entity
public class Order {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;

private float price; 

@ManyToOne(optional=false)
private Member member;

private String name;

所以我认为这是一个很好的配置,但是我在内存中的 HSQL 中测试了这个应用程序并且我得到了错误:

Hibernate: alter table Order drop constraint FK48E972E548C740B
2012-09-20 16:25:37 org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: alter table Order drop constraint FK48E972E548C740B
2012-09-20 16:25:37 org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: Blad skladniowy w wyrazeniu SQL "ALTER TABLE ORDER[*] DROP CONSTRAINT FK48E972E548C740B "; oczekiwano "identifier"
Syntax error in SQL statement "ALTER TABLE ORDER[*] DROP CONSTRAINT FK48E972E548C740B "; expected "identifier"; SQL statement:
alter table Order drop constraint FK48E972E548C740B [42001-165]

并且 :

Syntax error in SQL statement "CREATE TABLE ORDER[*] (ID INTEGER GENERATED BY DEFAULT AS IDENTITY, NAME VARCHAR(255), PRICE FLOAT NOT NULL, MEMBER_ID BIGINT NOT NULL, PRIMARY KEY (ID)) "; expected "identifier"; SQL statement:

我的 JUnit 测试失败了,我不知道这个配置有什么问题......

这是我的简单junit:

  @Test
public void testInsertWithOrder(){
    Order order = new Order(20.0f, "first stuff");
    Order order2 = new Order(40.0f, "secondary stuff");
    List<Order> orders = new ArrayList<Order>();
    orders.add(order2);
    orders.add(order);

    Member member = new Member("Member name", "member23@gmail.com", "2125552141", orders);

    memberDao.register(member);

    List<Member> members = memberDao.findAllOrderedByName();

    Assert.assertNotNull(members);
    Assert.assertEquals(1, members.size());     

}
4

3 回答 3

10

将表名从“订单”更改为不同的名称,例如 PersonOrder

于 2012-09-20T14:58:54.233 回答
1

在您memberOrder课堂上,缺少@JoinColumn注释。尝试如下。

@ManyToOne(optional=false)
@JoinColumn(name = "memberId", referencedColumnName = "id")
private Member member;
于 2012-09-20T15:00:18.090 回答
1

@CycDemo

我只是弄清楚了,在我的构造函数中,我现在得到了:

    @OneToMany(cascade=CascadeType.ALL , mappedBy="member" , fetch=FetchType.EAGER)
    private List<UOrder> orders = new ArrayList<UOrder>();



       public Member(String name, String email, String phoneNumber ,List<UOrder> orders) {
       super(name);
       this.orders = orders;
       this.email = email;
       for(UOrder o : orders){
           o.setMember(this);          
       }
       this.orders = orders;

   }

蚂蚁这就是我需要的:)))

于 2012-09-20T17:06:03.693 回答