Spring启动+数据JPA:未提取的已连接实体的属性

时间:2016-04-11 15:23:38

标签: java spring hibernate spring-boot spring-data-jpa

到目前为止,我使用了Spring web MVC +数据JPA,我可以用H2编写集成测试 现在我正在使用Spring Boot,当我在集成测试中尝试使用H2时,我有一个奇怪的行为。我有以下2个实体(也使用lombok):

 <!DOCTYPE html>
 <html>
 <head>
 <meta charset="utf-8">
 <title>Unemployment by Ward Bar Chart</title>

 <style type="text/css">
  .axis text{
  font-family: Arial;
  font-size: 13px;
  color: #333333;
  text-anchor: end;
  }

  path { 
  stroke: steelblue;
  stroke-width: 2;
  fill: none;
  }

 .axis path,
 .axis line {
 fill: none;
 stroke: grey;
 stroke-width: 1;
 shape-rendering: crispEdges;
 } 


 .textlabel{
    font-family:  Arial;
    font-size:13px;
    color: #333333;
    text-anchor: middle;
  }
}

</style>
<body>


 <script src="http://d3js.org/d3.v3.min.js"></script>

 <script>

 // Set the dimensions of the canvas / graph
 var margin = {top: 20, right: 0, bottom: 60, left: 60},

 width = 475;
 height = 350;
 padding = 100; 


  // Adds the svg canvas
  var svg = d3.select("body")
  .append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
    .append("g")
   .attr("viewBox", "0 0 " + width + " " + height);


 // Parse the date / time
 var parseDate = d3.time.format("%m/%d/%y").parse;
 var formatTax = d3.format(",.2f");

 // Set the ranges
 var x = d3.time.scale()
    .range([0, width - margin.right - margin.left], .1);


  var y = d3.scale.linear()
    .range([height - margin.top - margin.bottom, 0]);

 // Define the axes
 var xAxis = d3.svg.axis()
        .scale(x)
        .orient("bottom")
        .ticks(5);

 var yAxis = d3.svg.axis()
        .scale(y)
        .orient("left")
        .tickFormat(function(d) {return "$" + d + "B"});



 // Define the line
 var valueline = d3.svg.line()

.x(function(d) { return x(d.Date); })
.y(function(d) { return y(d["Tax Collections"]); });




// Get the data
 d3.csv("Yearly Tax Collections.csv", function(error, data) {
   data.forEach(function(d) {
    d.Date = parseDate(d.Date);
    d["Tax Collections"] = formatTax(+d["Tax Collections"]/1000000000);
});


 // Scale the range of the data
  x.domain(d3.extent(data, function(d) { return d.Date; }));
  y.domain([0, d3.max(data, function(d) { return d["Tax Collections"]; })]);

   // Add the valueline path.
     svg.append("path")
    .attr("class", "line")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
     .attr("d", valueline(data));

// Add the X Axis
svg.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(" + margin.left + "," + (height -         margin.bottom) + ")")
    .call(xAxis);

   // Add the Y Axis
   svg.append("g")
    .attr("class", "y axis")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
    .call(yAxis);

    // Y-axis labels
   svg.append("text")
   .attr("text-anchor", "middle")
   .style("font-size", "13px")
   .style("color", "#333333")
   .attr("transform", "translate ("+ (padding/4) + "," +(height/2)+") rotate(-90)")
   .text("Tax Revenue")
   .style("font-family", "Arial"); 

   // X-axis labels
   svg.append("text")
   .attr("text-anchor", "middle")
   .style("font-size", "13px")
   .style("color", "#333333")
   .attr("transform", "translate("+ (width/2) + "," +(height-(padding/4)) + ")")
   .text("Fiscal Year")
   .style("font-family", "Arial"); 

 //source

   svg.append("text")
   .attr("text-anchor", "middle")
   .style("font-size", "13px")
   .style("color", "#333333")
   .attr("transform", "translate("+ (width/4.5) + "," +(height/1) + ")")
   .text("Source: DC OCFO")
   .style("font-family", "Arial")

//title for the chart 

   svg.append("text")
   .attr("text-anchor", "middle")
   .style("font-size", "16px")
   .style("color", "#333333")
   .attr("transform", "translate("+ (width/3) + "," +(height/30) + ")")
   .text("DC Total Tax Revenues by Fiscal Year")
   .style("font-family", "Arial");


  svg.append("text")
  .attr("text-anchor", "left")
  .style("font-size", "10x")
  .style("color", "#333333")
  .attr("transform", "translate("+ (width/20) + "," +(height/12) + ")")
  .text("2000 to 2015")
  .style("font-family", "Arial")

  //line labels

  svg.append('g')
  .classed('labels-group', true)
  .selectAll('text')
  .data(data)
  .enter()

  .append('text')
  .filter(function(d, i) { return i === 0 || i === (data.length - 1) })
  .classed('label',true)
  .attr({
    'x':function(d,i) {
        return x(d.Date);

    },
      'y':function(d,i) {
        return y(d["Tax Collections"]);
    }
  })
 .text(function(d,i){
    return "$" + d["Tax Collections"] + "B";
   })
 .attr("transform", "translate(" + margin.left + "," + margin.top + ")") 



});

</script>
</body>

当我尝试将实体转换为DO时,我得到NullpointerException,因为只设置了Task的id,所有其他属性都为null。转换发生时我处于同一个事务中,因此应该加载属性。无论如何,我在调用转换器类之前尝试获取Task的'order'属性,但它是null。

@Entity
@Table(name = "EVENT_LIFE")
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class EventLifeEntity {

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "TASK_ID")
    private TaskEntity taskEntity;

    ...
}


@Entity
@Table(name = "TASK")
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class TaskEntity {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    @Column(name = "ORDR")
    private Integer order;

    @Column(name = "COMMAND")
    private String command;

    ...
}

Gradle依赖项:

@Service
public class RepositoryErrorSourceService implements ErrorSourceService {

    ....

    @Override
    @Transactional
    public String search(Event event) {
        ...
        EventLife eventLife = EventLife.builder()
                                       .date(Calendar.getInstance())
                                       .event(eventToDomainConverter.convert(eventEntity))
                                       .task(command)
                                       .eventStatus(EventStatusEnum.BEFORE_START)
                                       .order(0).build();
        EventLifeEntity eventLifeEntity = eventLifeEntityRepository.save( eventLifeToEntityConverter.convert(eventLife) );
        LOGGER.debug("The following EvenEntity is saved: {}", eventLifeEntity);
        // starting the first task of the workflow
        command.execute( eventLifeToDomainConverter.convert( eventLifeEntity) );
        ...
    }
}

所以我在集成测试中使用了很好的H2,但似乎我在春季启动时遗漏了一些东西。也有可能我忘记了某个地方的设置。无论哪种方式,我都被困住了 如果有人能帮助我那将是出色的!

谢谢,

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 忘了提,但我也试着用eager fetch类型。我甚至在调用转换器之前尝试引用特定属性但没有改变。

0 个答案:

没有答案