我有一个uni项目,旨在记录销售情况,并根据一套预先制定的规则,确定哪些符合四种附加条件:两个奖金(特定产品和销售人员的销售额)和两个奖项(产品活动和最佳销售人员)。 它使用Java(Spring Framework)编写,并使用Hibernate与表示层的MySql数据库和JSP进行通信。 在测试时,我发现销售奖金的某些东西不能正常工作。即使计算成功执行并且结果存储在数据库中,但在尝试检索它们时,我得到一个空值。
例如,执行计算时,jsp中显示的结果为
From | To | Salesman | Bonus | Prizes
| Sales | Product | Best salesman | Campaign
---------------------------------------------------------------------------------
01/01/2013 22/11/2013 Salesman 1 No No No No
01/01/2013 22/11/2013 Salesman 2 Yes Yes No Yes
01/01/2013 22/11/2013 Salesman 3 Yes Yes No No
01/01/2013 22/11/2013 Salesman 4 No No No No
但刷新后,包括从数据库中获取结果,只显示
From | To | Salesman | Bonus | Prizes
| Sales | Product | Best salesman | Campaign
---------------------------------------------------------------------------------
01/01/2013 22/11/2013 Salesman 2 Yes Yes No Yes
01/01/2013 22/11/2013 Salesman 3 No Yes No No
如果我通过PHPMyAdmin检查SalesBonus表,我会得到
id dateCreation dateFrom dateTo total units salesman_id
---------------------------------------------------------------------
1 2014-02-05 2013-01-01 2013-11-22 200 1 2
4 2014-02-05 2013-01-01 2013-11-22 200 3 3
此外,如果我使用Debug运行,变量树显示
AllBonuses ArrayList<E> (id=491)
elementData Object[3] (id=502)
[0] Aditional (id=503)
productCampaignRegistry Prize (id=506)
ProductBonus PersistentBag (id=507)
salesBonusRegistry SalesBonus (id=508)
dateCreation Timestamp (id=509)
dateFrom Timestamp (id=510)
dateTo Timestamp (id=511)
id 1
bestSalesman null
salesman Salesman (id=512)
active true
lastname "Noble" (id=513)
id 2
name "Donna" (id=514)
[1] Aditional (id=504)
productCampaignRegistry null
ProductBonus PersistentBag (id=515)
SalesBonusRegistry null (should be id 4 in the SalesBonus table)
dateCreation Timestamp (id=516)
dateFrom Timestamp (id=517)
dateTo Timestamp (id=518)
id 2
bestSalesman null
salesman Salesman (id=519)
active true
lastname "Dent" (id=520)
id 3
name "Arthur" (id=521)
表示已存储奖金表中显示的SalesBonus注册表,但在检索其所属的Aditional注册表时,它不会以某种方式出现。
为确保这是一个问题,我删除了整个数据库,再次创建它,插入一个新的Salesman和Sales并执行计算,以便它应插入一个新的SalesBonus注册表。不幸的是,这个问题一再重复。
这些课程是
@Entity
public class Aditional implements Serializable {
@Transient
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(nullable=false)
private Date dateCreation;
@Column(nullable=false)
private Date dateFrom;
@Column(nullable=false)
private Date dateTo;
@ManyToOne
private Salesman salesman;
@OneToOne
@JoinColumn(name="id")
private SalesBonus salesBonusRegistry;
@ManyToMany(cascade=CascadeType.ALL, fetch= FetchType.EAGER)
@Fetch(value = FetchMode.SUBSELECT)
private List<ProductBonus> productBonuses;
@OneToOne
@JoinColumn(name="id")
private Prize bestSalesman;
@OneToOne
@JoinColumn(name="id")
private Prize productCampaign;
@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
@DiscriminatorColumn(name="type", discriminatorType=DiscriminatorType.STRING)
public class Bonus implements Serializable {
@Transient
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private int id;
@Column(nullable=false)
private Date dateCreation;
@Column(nullable=false)
private Date dateFrom;
@Column(nullable=false)
private Date dateTo;
@ManyToOne(cascade=CascadeType.ALL, fetch= FetchType.EAGER)
private Salesman salesman;
@Column(nullable=false)
private int units=0;
@Column(nullable=false)
private float total=0;
@Entity
@DiscriminatorValue(value="S")
public class SalesBonus extends Bonus{
@Transient
private static final long serialVersionUID = 1L;
@ManyToMany(cascade=CascadeType.ALL, fetch= FetchType.EAGER)
@Fetch(value = FetchMode.SUBSELECT)
private List<Sale> elements;
有问题的servlet中使用的get方法是
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
setUser((User) request.getSession().getAttribute("user"));
if (getUser()==null)
{
response.sendRedirect(request.getContextPath() + "/login");
return;
}
if (!checkRole(getUser()))
{
response.sendRedirect(request.getContextPath() + "/error");
return;
}
request.setAttribute("salesmen", service.getActiveSalesmen());
request.setAttribute("products", service.getProducts());
request.setAttribute("aditionals", service.getAditionals());
request.getRequestDispatcher("/WEB-INF/ComputeAditionals.jsp").forward(request, response);
}
这是服务层中用于检索Aditional registries
的方法@Override
public ArrayList<Aditional> getAditionals() {
ArrayList<Aditional> all = dataAccess.getAditionals();
for (Aditional aditional : all)
{
if (aditional.getCampaign()!=null && aditional.getCampaign().getProduct()==null)
aditional.setCampaign(null);
if (aditional.getBestSalesman()!=null && aditional.getBestSalesman().getProduct()!=null)
aditional.setBestSalesman(null);
}
return all;
}
这是DataAcessHibernateTemplate
中使用的方法@Override
public ArrayList<Aditional> getAditionals()
{
return (ArrayList<Aditional>) this.hibernateTemplate.loadAll(Aditional.class);
}
在尝试检索ProductBonus注册表时,它也使用此
@Override
public ProductBonus getProductBonus(Integer id)
{
return this.hibernateTemplate.get(ProductBonus.class, id);
}
先谢谢!
编辑:这是我在加载Adicional注册表时得到的(稍加编辑的)SLF4j输出
18:34:33,853 DEBUG AbstractBatcher:410 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
18:34:33,853 DEBUG ConnectionManager:444 - opening JDBC connection
18:34:33,881 DEBUG SQL:111 -
select
this_.id as id5_6_,
this_.creationDate as creation2_5_6_,
this_.dateFrom as dateFrom5_6_,
this_.dateTo as dateTo5_6_,
this_.salesman_id as salesman5_5_6_,
salesman2_.id as id3_0_,
salesman2_.active as active3_0_,
salesman2_.lastname as lastname3_0_,
salesman2_.name as name3_0_,
prize3_.id as id9_1_,
prize3_.creationDate as creation2_9_1_,
prize3_.dateFrom as dateFrom9_1_,
prize3_.dateTo as dateTo9_1_,
prize3_.total as total9_1_,
prize3_.isCampaign as isCampaign9_1_,
prize3_.prizeWinner_id as prizeWin7_9_1_,
prize3_.product_id as product8_9_1_,
salesman4_.id as id3_2_,
salesman4_.active as active3_2_,
salesman4_.lastname as lastname3_2_,
salesman4_.name as name3_2_,
product5_.id as id2_3_,
product5_.name as name2_3_,
product5_.costUnit as costUnit3_2_3_,
salesBonus6_.id as id6_4_,
salesBonus6_.creationDate as creation2_6_4_,
salesBonus6_.dateFrom as dateFrom6_4_,
salesBonus6_.dateTo as dateTo6_4_,
salesBonus6_.total as total6_4_,
salesBonus6_.units as units6_4_,
salesBonus6_.salesman_id as salesman7_6_4_,
salesman7_.id as id3_5_,
salesman7_.active as active3_5_,
salesman7_.lastname as lastname3_5_,
salesman7_.name as name3_5_
from
(Adicional this_ left outer join Salesman salesman2_ on
(this_.salesman_id=salesman2_.id left outer join Prize prize3_ on
(this_.id=prize3_.id left outer join Salesman salesman4_ on
(prize3_.prizeWinner_id=salesman4_.id left outer join Product product5_ on
(prize3_.product_id=product5_.id left outer join SalesBonus salesBonus6_ on
(this_.id=salesBonus6_.id left outer join Salesman salesman7_ on salesBonus6_.salesman_id=salesman7_.id))))))
18:34:33,881 TRACE AbstractBatcher:513 - preparing statement
18:34:33,886 DEBUG AbstractBatcher:426 - about to open ResultSet (open ResultSets: 0, globally: 0)
18:34:33,890 TRACE BasicExtractor:71 - found [2] as column [id3_0_]
18:34:33,891 TRACE BasicExtractor:66 - found [null] as column [id9_1_]
18:34:33,891 TRACE BasicExtractor:66 - found [null] as column [id3_2_]
18:34:33,891 TRACE BasicExtractor:66 - found [null] as column [id2_3_]
18:34:33,892 TRACE BasicExtractor:71 - found [1] as column [id6_4_]
18:34:33,892 TRACE BasicExtractor:71 - found [2] as column [id3_5_]
18:34:33,893 TRACE BasicExtractor:71 - found [1] as column [id5_6_]
18:34:33,895 TRACE BasicExtractor:71 - found [true] as column [active3_0_]
18:34:33,896 TRACE BasicExtractor:71 - found [Noble] as column [lastname3_0_]
18:34:33,896 TRACE BasicExtractor:71 - found [Donna] as column [name3_0_]
18:34:33,907 TRACE BasicExtractor:71 - found [2013-12-20 20:17:34.0] as column [creation2_6_4_]
18:34:33,908 TRACE BasicExtractor:71 - found [2013-01-01 00:00:00.0] as column [dateFrom6_4_]
18:34:33,908 TRACE BasicExtractor:71 - found [2013-11-22 00:00:00.0] as column [dateTo6_4_]
18:34:33,909 TRACE BasicExtractor:71 - found [200.0] as column [total6_4_]
18:34:33,909 TRACE BasicExtractor:71 - found [1] as column [units6_4_]
18:34:33,909 TRACE BasicExtractor:71 - found [2] as column [salesman7_6_4_]
18:34:33,910 TRACE BasicExtractor:71 - found [2013-12-20 20:17:34.0] as column [creation2_5_6_]
18:34:33,910 TRACE BasicExtractor:71 - found [2013-01-01 00:00:00.0] as column [dateFrom5_6_]
18:34:33,910 TRACE BasicExtractor:71 - found [2013-11-22 00:00:00.0] as column [dateTo5_6_]
18:34:33,910 TRACE BasicExtractor:71 - found [2] as column [salesman5_5_6_]
18:34:33,911 TRACE BasicExtractor:71 - found [3] as column [id3_0_]
18:34:33,911 TRACE BasicExtractor:66 - found [null] as column [id9_1_]
18:34:33,911 TRACE BasicExtractor:66 - found [null] as column [id3_2_]
18:34:33,911 TRACE BasicExtractor:66 - found [null] as column [id2_3_]
18:34:33,911 TRACE BasicExtractor:66 - found [null] as column [id6_4_]
18:34:33,912 TRACE BasicExtractor:66 - found [null] as column [id3_5_]
18:34:33,912 TRACE BasicExtractor:71 - found [2] as column [id5_6_]
18:34:33,912 TRACE BasicExtractor:71 - found [true] as column [active3_0_]
18:34:33,912 TRACE BasicExtractor:71 - found [Dent] as column [lastname3_0_]
18:34:33,912 TRACE BasicExtractor:71 - found [Arthur] as column [name3_0_]
18:34:33,913 TRACE BasicExtractor:71 - found [2013-12-20 20:17:34.0] as column [creation2_5_6_]
18:34:33,913 TRACE BasicExtractor:71 - found [2013-01-01 00:00:00.0] as column [dateFrom5_6_]
18:34:33,913 TRACE BasicExtractor:71 - found [2013-11-22 00:00:00.0] as column [dateTo5_6_]
18:34:33,914 TRACE BasicExtractor:71 - found [3] as column [salesman5_5_6_]
18:34:33,914 DEBUG AbstractBatcher:433 - about to close ResultSet (open ResultSets: 1, globally: 1)
18:34:33,914 DEBUG AbstractBatcher:418 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
18:34:33,914 TRACE AbstractBatcher:562 - closing statement
18:34:33,923 DEBUG AbstractBatcher:410 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
这让我觉得它可能与sql查询有关,虽然如何解释只加载第一个注册表的事实? = \
答案 0 :(得分:0)
解决。这是一个映射问题。添加正确的连接列并重新映射数据库就可以了。
因此,修改后的Aditional类现在是。
@Entity
public class Aditional implements Serializable {
@Transient
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(nullable=false)
private Date dateCreation;
@Column(nullable=false)
private Date dateFrom;
@Column(nullable=false)
private Date dateTo;
@ManyToOne
private Salesman salesman;
@OneToOne
@JoinColumn(name="salesBonus_id")
private SalesBonus salesBonusRegistry;
@ManyToMany(cascade=CascadeType.ALL, fetch= FetchType.EAGER)
@Fetch(value = FetchMode.SUBSELECT)
private List<ProductBonus> productBonuses;
@OneToOne
@JoinColumn(name="salesmanPrize_id")
private Prize bestSalesman;
@OneToOne
@JoinColumn(name="campaignPrize_id")
private Prize productCampaign;