0

i use hibernate deal with entity. the classes are

@Entity
@Table(name="bus_line")
@NamedQuery(name="BusLine.findAll", query="SELECT b FROM BusLine b")
public class BusLine implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(unique=true, nullable=false)
    private Long id;

    @Column(nullable=false)
    private Long firstExpressTime;

    @Column(nullable=false)
    private Long lastExpressTime;

    private Integer lineDirection;

    @Column(nullable=false, length=10)
    private String lineName;

    @Column(nullable=false)
    private Long originatingStationID;

    @Column(nullable=false, length=10)
    private String originatingStationName;

    @Column(nullable=false)
    private Long terminusID;

    @Column(nullable=false, length=10)
    private String terminusName;

    @ManyToMany(mappedBy="busLines")
    private List<BusStation> busStations = new ArrayList<BusStation>();
    omit getter and setter method
}

@Entity
@Table(name="bus_station")
@NamedQuery(name="BusStation.findAll", query="SELECT b FROM BusStation b")
public class BusStation implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(unique=true, nullable=false)
    private Long id;

    @Column(nullable=false)
    private Double stationLat;

    @Column(nullable=false)
    private Double stationLng;

    @Column(nullable=false, length=20)
    private String stationName;

    @JoinTable(name="bus_line_to_station",joinColumns = @JoinColumn(name="stationID"),
            inverseJoinColumns=@JoinColumn(name="lineID"))
    private List<BusLine> busLines = new ArrayList<BusLine>();

    omit setter and getter method
}

the table is

   CREATE TABLE `bus_line` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT ,
  `lineName` varchar(10) NOT NULL ,
  `originatingStationName` varchar(10) NOT NULL ,
  `originatingStationID` bigint(20) NOT NULL ,
  `terminusID` bigint(20) NOT NULL ,
  `terminusName` varchar(10) NOT NULL ,
  `firstExpressTime` datetime NOT NULL ,
  `lastExpressTime` datetime NOT NULL ,
  `lineDirection` int(3) DEFAULT '1' ,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `bus_line_to_station` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT ,
  `lineID` bigint(20) NOT NULL ,
  `stationID` bigint(20) NOT NULL ,
  `stationOrder` int(3) NOT NULL ,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `bus_station` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT ,
  `stationName` varchar(20) NOT NULL ,
  `stationLng` double NOT NULL ,
  `stationLat` double NOT NULL ,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

but i worry that if i divide one table into many sub tables,such as

bus_station divided into bus_station1,bus_station2,bus_station3 and so on

bus_line_to_station divided into bus_line_to_station1,bus_line_to_station2,bus_line_to_station3 and so on in the same database.

the hibernate how to deal with the situation?

or i create other two databases,and create table bus_line_to_station1,bus_line_to_station2 ...in database1,and create table bus_station1,bus_station2 ... in database2.

the hibernate how to deal with the situation? thanks for your any help and suggestion in advance.

王奕然
  • 3,891
  • 6
  • 41
  • 62
  • What you are asking here does not seem to make much sense so probably I do not understand what you want. Please clarify why you want to create additional tables or even db's for what seems to be a straightforward Many2Many relationship (where you would just have 1 additional bus_line_to_station table for the mapping. – Stijn Geukens Nov 14 '14 at 10:37
  • thanks your suggestion,i've add the table design – 王奕然 Nov 14 '14 at 10:44

1 Answers1

0

Based on your information you cannot use a ManyToMany since you want to keep additional information on the relation between a bus and a station. In this case you need to create a mapping entity like BusLineToStation with following properties:

  • Busline (ManyToOne)
  • Station (ManyToOne)
  • stationOrder (Integer)
  • id (optional)

From BusLine and BusStation you can then just have a OneToMany to BustLineToStation.

See also https://stackoverflow.com/a/5127262/190596

Community
  • 1
  • 1
Stijn Geukens
  • 15,454
  • 8
  • 66
  • 101