我使用hibernate处理实体。 这些课程是
@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
}
表格是
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;
但我担心如果我将一个表分成许多子表,例如
bus_station分为bus_station1,bus_station2,bus_station3等
bus_line_to_station在同一个数据库中分为bus_line_to_station1,bus_line_to_station2,bus_line_to_station3等。
hibernate如何应对这种情况?或者我创建其他两个数据库,并在database1中创建表bus_line_to_station1,bus_line_to_station2 ...,并在database2中创建表bus_station1,bus_station2 ....
hibernate如何应对这种情况? 感谢您的任何帮助和建议。答案 0 :(得分:0)
根据您的信息,您不能使用ManyToMany,因为您希望保留有关总线和工作站之间关系的其他信息。在这种情况下,您需要创建一个具有以下属性的BusLineToStation等映射实体:
从BusLine和BusStation,您可以拥有OneToMany到BustLineToStation。