我有一个应用程序(Spring 4 MVC + Hibernate 4+使用注释的MySQL + Maven集成示例),使用基于注释的配置将Spring与Hibernate集成。
我有这个实体:
@Entity
@DiscriminatorValue(value = "Application")
@NamedQueries(value = {})
public class ApplicationAlarm extends Alarm implements Serializable {
/* */
private static final long serialVersionUID = -507197670530074142L;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "application_id")
private Application application;
/**
* Gets the application.
*
* @return the application
*/
public Application getApplication() {
return application;
}
/**
* Sets the application.
*
* @param p_application
* the new application
*/
public void setApplication(final Application p_application) {
application = p_application;
}
}
和此:
@Entity
@Table(name = "alarm")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type")
@NamedQueries(value = {})
public abstract class Alarm implements Serializable {
/* */
private static final long serialVersionUID = -3210359168688290412L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Enumerated(EnumType.STRING)
@Column(name = "action",
nullable = false)
private AlarmAction action;
@Enumerated(EnumType.STRING)
@Column(name = "level",
nullable = false)
private AlarmLevel level;
@Column(name = "type",
nullable = false,
insertable = false,
updatable = false)
private String type;
@ManyToMany(fetch = FetchType.LAZY,
mappedBy = "alarms")
private List<Guardian> guardians = new ArrayList<Guardian>();
@ManyToMany(fetch = FetchType.LAZY,
mappedBy = "alarms")
private List<TimeLapse> timeLapses = new ArrayList<TimeLapse>();
@Version
@Column(name = "version")
private Long version = new Long(0);
/**
* Gets the id.
*
* @return the id
*/
public Long getId() {
return id;
}
/**
* Sets the id.
*
* @param p_id
* the new id
*/
public void setId(final Long p_id) {
id = p_id;
}
/**
* Gets the action.
*
* @return the action
*/
public AlarmAction getAction() {
return action;
}
/**
* Sets the action.
*
* @param p_action
* the new action
*/
public void setAction(final AlarmAction p_action) {
action = p_action;
}
/**
* Gets the level.
*
* @return the level
*/
public AlarmLevel getLevel() {
return level;
}
/**
* Sets the level.
*
* @param p_level
* the new level
*/
public void setLevel(final AlarmLevel p_level) {
level = p_level;
}
/**
* Gets the type.
*
* @return the type
*/
public String getType() {
return type;
}
/**
* Sets the type.
*
* @param p_type
* the new type
*/
public void setType(final String p_type) {
type = p_type;
}
/**
* Gets the guardians.
*
* @return the guardians
*/
public List<Guardian> getGuardians() {
return guardians;
}
/**
* Sets the guardians.
*
* @param p_guardians
* the new guardians
*/
public void setGuardians(final List<Guardian> p_guardians) {
guardians = p_guardians;
}
public List<TimeLapse> getTimeLapses() {
return timeLapses;
}
public void setTimeLapses(List<TimeLapse> timeLapses) {
this.timeLapses = timeLapses;
}
/**
* Gets the version.
*
* @return the version
*/
public Long getVersion() {
return version;
}
/**
* Sets the version.
*
* @param p_version
* the new version
*/
public void setVersion(final Long p_version) {
version = p_version;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(final Object p_obj) {
boolean isEquals = false;
try {
final ApplicationAlarm application = (ApplicationAlarm) p_obj;
final EqualsBuilder eb = new EqualsBuilder();
eb.append(getAction(), application.getAction());
eb.append(getLevel(), application.getLevel());
eb.append(getType(), application.getType());
isEquals = eb.isEquals();
} catch (final Exception e) {
isEquals = false;
}
return isEquals;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final HashCodeBuilder hcb = new HashCodeBuilder();
hcb.append(getAction());
hcb.append(getLevel());
hcb.append(getType());
return hcb.toHashCode();
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
ToStringBuilder tsb = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
tsb.append("id", getId());
tsb.append("action", getAction());
tsb.append("level", getLevel());
//tsb.append("guardians", getGuardians());
tsb.append("type", getType());
//tsb.append("guardians", getGuardians());
tsb.append("version", getVersion());
return tsb.toString();
}
}
JSP上的:
<form:form id="alarmRegisterForm"
cssClass="form-horizontal"
modelAttribute="alarm"
method="post"
action="${pageContext.request.contextPath}/alarm/save" >
<form:hidden path="id" value="${alarm.id}"/>
<div class="form-group">
<div class="control-label col-xs-3"><form:label path="action" >Application</form:label> </div>
<div class="col-xs-6">
<form:select path="application" items="${applications}" itemValue="id" itemLabel="description" />
</div>
</div>
<div class="form-group">
<div class="control-label col-xs-3"><form:label path="action" >Action</form:label> </div>
<div class="col-xs-6">
<form:select path="action">
<form:options items="${alarmActions}" />
</form:select>
</div>
</div>
<div class="form-group">
<div class="control-label col-xs-3"><form:label path="level" >Level</form:label> </div>
<div class="col-xs-6">
<form:select path="level">
<form:options items="${alarmLevels}" />
</form:select>
</div>
</div>
<div class="form-group">
<div class="control-label col-xs-3"><form:label path="level" >Time Lapse</form:label> </div>
<div class="col-xs-6">
<form:select path="timeLapses" items="${timeLapses}" itemValue="id" itemLabel="name" />
</div>
</div>
<div class="form-group">
<div class="control-label col-xs-3"><form:label path="level" >Guardians</form:label> </div>
<div class="col-xs-6">
<form:select path="guardians" items="${guardians}" itemValue="id" itemLabel="name" />
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-xs-4">
<input type="button" id="Cancel" class="btn btn-primary" value="Cancel" />
</div>
<div class="col-xs-4">
<input type="submit" id="saveAlarm" class="btn btn-primary" value="Save" />
</div>
<div class="col-xs-4">
</div>
</div>
</div>
</form:form>
控制器:
@RequestMapping(value = { "/save" }, method = RequestMethod.POST)
public String saveAlarm(/* @Valid */ final ApplicationAlarm alarm, final BindingResult result, final ModelMap model) {
LOGGER.info("Saving the Alarm : " + alarm);
if(alarm.getId() == null){
getAlarmMutatorService().save (alarm);
} else {
getAlarmMutatorService().update(alarm);
}
return "alarm.list";
}
但是没有映射属性应用程序,因为它是null
答案 0 :(得分:0)
select只能指向一个值,因为application
是一个对象,select无法将其映射到相应的对象。
相反,在jsp中,您可以尝试将application
更改为application.id
并更改选择输入,使其返回您选择的应用程序的ID。