本文介绍了Spring Roo,Hibernate,一对多关系创建额外的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试图用1:M关系创建两个表。



例如,
我需要两个实体Person和Car。一个人可以有很多车。



我使用Roo创建了实体

  entity --class〜.domain.Person 
字段字符串名称
实体--class〜.domain.Car
字段字符串名称
字段引用--fieldName所有者 - -type〜.domain.Person
field set --fieldName ownedCars --type〜.domain.Car --class〜.domain.Person --cardinality ONE_TO_MANY

汽车的生成类:

  @RooJavaBean 
@RooToString
@RooEntity
公共类汽车{

私有字符串名称;

@ManyToOne
私人所有者;
}

为Person生成的类

  @RooJavaBean 
@RooToString
@RooEntity
public class Person {

private String Name;

@OneToMany(cascade = CascadeType.ALL)
private Set< Car> ownedCars = new HashSet< Car>();
}

然而,在数据库中有3个表格/ p>

表CAR(与预期的一样)

  CREATE TABLETEST。 CAR

IDNUMBER(19,0),
NAMEVARCHAR2(255 BYTE),
VERSIONNUMBER(10,0),
OWNERNUMBER(19,0)

预计)

  CREATE TABLETEST。PERSON

IDNUMBER(19 ,0),
NAMEVARCHAR2(255 BYTE),
VERSIONNUMBER(10,0)


以及PERSON_OWNED_CARS(这不是预期的,它不是多对多关系)

$ pre code $ c> CREATE TABLETEST。PERSON_OWNED_CARS

PERSONNUMBER(19,0),
OWNED_CARSNUMBER(19,0)

为什么会生成最后一张表格?最后一张桌子的目的是什么?这不是多对多的关系?可以避免吗?我做错了什么?

解决方案

我不确定Roo如何管理这一点,但您需要链接双向关系与 mappedBy

  @OneToMany(cascade = CascadeType.ALL,mappedBy =所有者)
私人设置< Car> ownedCars = new HashSet< Car>();

否则它们被解释为两个不同的单向关系,并且来自 Person Car 通过连接表实现(这是单向一对多关系的默认行为)。


I'm new to spring mvc, roo and hibernate.

I'm trying to create two tables with 1:M relationship.

For example,I want two entities, Person and Car. One person can have many cars.

I've created entities using Roo

entity --class ~.domain.Person
field string Name
entity --class ~.domain.Car
field string Name
field reference --fieldName owner --type ~.domain.Person
field set --fieldName ownedCars --type ~.domain.Car --class ~.domain.Person --cardinality ONE_TO_MANY 

Generated class for car:

@RooJavaBean
@RooToString
@RooEntity
public class Car {

    private String Name;

    @ManyToOne
    private Person owner;
}

Generated class for Person

@RooJavaBean
@RooToString
@RooEntity
public class Person {

    private String Name;

    @OneToMany(cascade = CascadeType.ALL)
    private Set<Car> ownedCars = new HashSet<Car>();
}

However, in the database, there are 3 tables (insted of two)

Table CAR (as expected)

  CREATE TABLE "TEST"."CAR" 
   (    
    "ID" NUMBER(19,0), 
    "NAME" VARCHAR2(255 BYTE), 
    "VERSION" NUMBER(10,0), 
    "OWNER" NUMBER(19,0)
   )

Table PERSON (as expected)

  CREATE TABLE "TEST"."PERSON" 
   (
"ID" NUMBER(19,0), 
    "NAME" VARCHAR2(255 BYTE), 
    "VERSION" NUMBER(10,0)
   )

and also PERSON_OWNED_CARS (which is not expected, it's not many to many relationship)

  CREATE TABLE "TEST"."PERSON_OWNED_CARS" 
   (
"PERSON" NUMBER(19,0), 
    "OWNED_CARS" NUMBER(19,0)
   )

Why is the last table generated? What is the purpose of the last table, it's not many to many relationship? Can it be avoided? Am I doing something wrong?

解决方案

I'm not sure how Roo manages this, but you need to link sides of bidirectional relationships with mappedBy:

@OneToMany(cascade = CascadeType.ALL, mappedBy = "owner")
private Set<Car> ownedCars = new HashSet<Car>();

Otherwise they are interpreted as two different unidirectional relationships, and relationship from Person to Car is implemented via join table (it's a default behaviour for unidirectional one-to-many relationships).

这篇关于Spring Roo,Hibernate,一对多关系创建额外的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 10:48