java - 如何避免加入并方便扩展

标签 java hibernate dao

我有三张 table 一个是公交车站 其他是公交线路 巴士线包含许多巴士站, 我想使用 hibernate 来映射连接,但我的老板让我使用 busStation_to_busLine 表与这两个表关联。 他说这种方式转换为hbase很方便(目前我们使用mysql)。 所以我必须选择busStation,然后选择busStation_to_busLine,然后选择busLine,才能得到我想要的。 如何让我的代码更简单,不选择三个表,并且不受 hibernate 连接问题的困扰。 那么我应该使用什么dao框架,或者如何让我的代码更容易编写?或者设计更好的表结构?

更新 它报告异常

Caused by: org.hibernate.MappingException: Could not determine type for: java.util.List, at table: bus_station, for columns: [org.hibernate.mapping.Column(busLines)]

我的实体和表是

@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;

最佳答案

您可以使用@ManyToMany来映射BusStationBusLine之间的关系

@Entity
public class BusStation {
    ...
    @ManyToMany
    @JoinTable(name = "busStation_to_busLine", joinColumns = @JoinColumn(name = "busStation_id"),
      inverseJoinColumns = @JoinColumn(name = "busLine_id")
    private List<BusLine> busLines = new ArrayList<BusLine>();
    ...
}

@Entity
public class BusLine {
    ...
    @ManyToMany(mappedBy = "busLines")
    private List<BusStation> busStations = new ArrayList<BusStation>();
    ...
}

关于java - 如何避免加入并方便扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26883875/

相关文章:

java - 基于搜索字符串插入文本的字符串正则表达式

java - 与jsp的数据库连接

java - jboss eap-6.1 无法处理部署的 POST_MODULE 阶段\"education.war\"

java - 强制 hibernate 读取数据库并且不返回缓存的实体

java - 高级Java泛型问题: why do we need to specify redundant information

java - 在简单的数据库驱动的 Java Web 应用程序中使用异常

java - 我怎样才能在java中连接两个字节?

java - 将消息从多个 Activity 传递到主要 Activity

java - Hibernate:通过注释具有动态表名的数据对象

java - 如何设计一个DAO更新方法