Dao为空,但只有一种情况



对于Spring(和StackOverflow(来说还是相当新的,所以如果这里有明显的答案,请原谅我。

我正在做的项目有三个模型类:星,位置,观测。我已经将前两个连接到MySQL,并且运行良好。在继续之前,我正在测试每一个,所以第三个还没有,并且暂时使用ArrayList来存储观测值。但是,观测需要从位置检索信息。但当我试图通过LocationDao获取数据时,它总是返回NULL。然而,在我的一个控制器中,我需要访问位置数据来显示它,它工作得很好。我看不出两者有什么区别。

相关代码:

位置模型类

package StarCatalog.models;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
@Entity
public class Location {
@Id
@GeneratedValue
private int locationId;
@NotNull
@Size(min=3, max=25, message="Name out of bounds")
private String locationName;
@NotNull(message = "Cannot be blank")
private Double latitude;
@NotNull(message = "Cannot be blank")
private Double longitude;
// Constructors
public Location() { }
public Location(String aLocation, Double aLatitude, Double aLongitude) {
locationName = aLocation;
latitude = aLatitude;
longitude = aLongitude;
}
// Getters & Setters
public int getLocationId() {
return locationId;
}
public String getLocationName() {
return locationName;
}
public void setLocationName(String location) {
this.locationName = location;
}
public Double getLatitude() {
return latitude;
}
public void setLatitude(Double latitude) {
this.latitude = latitude;
}
public Double getLongitude() {
return longitude;
}
public void setLongitude(Double longitude) {
this.longitude = longitude;
}
}

位置刀

package StarCatalog.models.data;
import StarCatalog.models.Location;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import javax.transaction.Transactional;
@Repository
@Transactional
public interface LocationDao extends CrudRepository<Location, Integer> {
}

观测模型类

package StarCatalog.models;
import StarCatalog.models.data.LocationDao;
import org.springframework.beans.factory.annotation.Autowired;
import javax.validation.constraints.NotNull;
public class Observation {
@Autowired
private LocationDao locationDao;
// Primary Key
private int observationId;
private static int nextId = 0;
// Altitude
@NotNull
private Double altitude;
// Azimuth
@NotNull
private Double azimuth;
// ST
@NotNull
private int siderealTimeH;
@NotNull
private int siderealTimeM;
private Double siderealTimeDeg;
// Lat
private Double latitude;
// RA
private Double rightAscension;
// Dec
private Double declination;
// Foreign key of Location
@NotNull
private int locationId;
// Foreign key of Star
@NotNull
private Integer objectId;
// Constructor
public Observation() {
observationId = nextId;
nextId = nextId + 1;
}
// Calculators
public void setLatitude() {
latitude = locationDao.findOne(locationId).getLatitude(); // Error here - locationDao = null
}
// Cut non-applicable code

StarController(在哪里工作(

package StarCatalog.controllers;
import StarCatalog.models.*;
import StarCatalog.models.data.LocationDao;
import StarCatalog.models.data.StarDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import javax.validation.Valid;
import java.util.ArrayList;
@Controller
@RequestMapping("star")
public class StarController {
@Autowired
private StarDao starDao;
@Autowired
private LocationDao locationDao;
// Index
@RequestMapping("")
public String index(Model model) {
Iterable<Star> stars = starDao.findAll();
ArrayList<Observation> observations = ObservationData.getAll();
Iterable<Location> locations = locationDao.findAll();  //This one works just fine.
model.addAttribute("stars", stars);
model.addAttribute("observations", observations);
model.addAttribute("locations", locations);
model.addAttribute("title", "Uranometria 2.0");
return "star/index";
}

我试着把Observation中的findOne换成findAll,以防出现问题,但由于是locationDao返回null,所以我使用哪一个都无关紧要。

如果它有帮助的话,下面是它抛出的错误:

java.lang.NullPointerException: null
at StarCatalog.models.Observation.setLatitude(Observation.java:58) ~[main/:na]

首先,您的整个设计有点偏离。

  • 您似乎使用了一个名为Location的表,该表映射到相应的数据库表,然后是一个名为Observation的类存储与此相关的数据的CCD_ 3表的超级表,以及具有对位置表的引用。

  • 您现有的不是有效的表层次结构设计。理想情况下Observation表应该是顶级实体,与CCD_ 5表具有一对多关系。你可以在线查看如何很容易地映射这种关系。

  • 您不需要额外的ObservationData类来包装arraylist在那里检索位置。一对多设计,您的Observation类将包含一个列表,或者更好的是集合,该集合将包含每个观测的位置信息。

  • LocationDaoObservation中为空的原因与事实上,有问题的类不是Spring管理的豆因此,Spring不知道如何注入有问题的beanCCD_ 10。您可以通过注释"观察"来解决此问题类,@Component将其引入Spring上下文因此允许你这样做,但我建议你不要这样做(请参阅上面关于如何正确映射它们之间的关系的内容(。

  • LocationDao这显然不是一个DAO,因此命名惯例是错误的。也不需要@Transactional注释。

  • 避免通过现场注入注入bean,更喜欢通过构造函数。

我希望以上所有的帮助。

最新更新