问题:
org.springframework.beans.NotReadablePropertyException: Invalid property 'Orgaddress' of bean class
[com.techvision.studycenter.model.Organization]: Bean property 'Orgaddress' is not readable or has an invalid getter method:
Does the return type of the getter match the parameter type of the setter?
OrgController.java
package com.techvision.studycenter.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
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.servlet.ModelAndView;
import com.techvision.studycenter.model.Organization;
import com.techvision.studycenter.service.OrgService;
@Controller
public class OrgController {
@Autowired
private OrgService orgService;
@RequestMapping(value="/login")
public ModelAndView home(){
return new ModelAndView("OrgList", "command", new Organization());
}
@RequestMapping(value="/saveOrg")
public ModelAndView saveOrg(@ModelAttribute("orgnew")Organization organization)
{
orgService.saveOrg(organization);
return new ModelAndView("redirect:/add.html");
}
@RequestMapping(value = "/add", method = RequestMethod.GET)
public ModelAndView addEmployee(@ModelAttribute("orgnew")Organization organization,
BindingResult result) {
Map<String, Object> model = new HashMap<String, Object>();
model.put("orgLists", orgService.listOrgs());
return new ModelAndView("orgList", model);
}
}
OrgService.java
package com.techvision.studycenter.service;
import java.util.List;
import org.springframework.stereotype.Service;
import com.techvision.studycenter.model.Organization;
public interface OrgService {
void saveOrg(Organization organization);
List<Organization> listOrgs();
}
OrgServiceImpl.java
package com.techvision.studycenter.service;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.techvision.studycenter.model.Organization;
@Service("OrgService")
@Transactional
public class OrgSerivceImpl implements OrgService {
@Autowired
private SessionFactory sessionFactory;
@Override
public void saveOrg(Organization organization) {
// TODO Auto-generated method stub
System.out.println("i am at service");
sessionFactory.getCurrentSession().saveOrUpdate(organization);
}
@Override
public List<Organization> listOrgs() {
// TODO Auto-generated method stub
return (List<Organization>) sessionFactory.getCurrentSession().createCriteria(Organization.class).list();
}
}
JPA实体
Organization.java
package com.techvision.studycenter.model;
import java.io.Serializable;
import javax.persistence.*;
import java.util.Date;
import java.math.BigInteger;
import java.util.Set;
/**
* The persistent class for the organization database table.
*
*/
@Entity
@Table(name="organization")
@NamedQuery(name="Organization.findAll", query="SELECT o FROM Organization o")
public class Organization implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(unique=true, nullable=false)
private long orgid;
@Temporal(TemporalType.TIMESTAMP)
private Date dateofcreation;
@Column(length=45)
private String orgcode;
@Column(length=45)
private String orgmode;
@Column(length=45)
private String orgname;
private long orgparent;
@Column(length=1)
private char orgstatus;
//bi-directional many-to-one association to Degreecategory
@OneToMany(mappedBy="organization")
private Set<Degreecategory> degreecategories;
//bi-directional many-to-one association to Medium
@OneToMany(mappedBy="organization")
private Set<Medium> mediums;
//bi-directional many-to-one association to Orgaddress
@OneToMany(mappedBy="organization")
private Set<Orgaddress> orgaddresses;
//bi-directional many-to-one association to Person
@OneToMany(mappedBy="organization")
private Set<Person> persons;
//bi-directional many-to-one association to Yearname
@OneToMany(mappedBy="organization")
private Set<Yearname> yearnames;
public Organization() {
}
//getter and setter methods...
}
OrgAddress.java
package com.techvision.studycenter.model;
import java.io.Serializable;
import javax.persistence.*;
import java.util.Date;
/**
* The persistent class for the orgaddress database table.
*
*/
@Entity
@Table(name="orgaddress")
@NamedQuery(name="Orgaddress.findAll", query="SELECT o FROM Orgaddress o")
public class Orgaddress implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(unique=true, nullable=false)
private long orgaddressid;
@Column(length=100)
private String address1;
@Column(length=100)
private String address2;
@Column(length=100)
private String city;
@Column(length=45)
private String country;
@Temporal(TemporalType.TIMESTAMP)
private Date dateofcreation;
@Column(length=200)
private String emailid1;
@Column(length=200)
private String emailid2;
@Column(length=45)
private String fax;
@Column(length=45)
private String landlinenumber;
@Column(length=100)
private String location;
@Column(length=45)
private String mobilenumber;
@Column(length=45)
private String orgaddresscol;
@Column(length=1)
private char orgaddressstatus;
@Column(length=45)
private String postalcode;
@Column(length=45)
private String state;
//bi-directional many-to-one association to Organization
@ManyToOne
@JoinColumn(name="orgid", nullable=false)
private Organization organization;
public Orgaddress() {
}
//getter and setter method...
}
OrgList.jsp
<form:input path="orgcode" id="orgcode" name="orgcode" type="text" placeholder="Org Code" class="form-control input-md"/>
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">Street1</label>
<div class="col-md-4">
<form:input path="Orgaddress.address1" id="Orgaddress.address1" type="text" placeholder="placeholder" class="form-control input-md"/>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">Street 2</label>
<div class="col-md-4">
<form:input path="Orgaddress.address2" id="Orgaddress.address2" name="Orgaddress.address2" type="text" placeholder="placeholder" class="form-control input-md"/>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">City</label>
<div class="col-md-4">
<form:input path="orgaddresses.city" id="orgaddresses.city" name="city" type="text" placeholder="placeholder" class="form-control input-md"/>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">Country</label>
<div class="col-md-4">
<form:input path="orgaddresses.country" id="orgaddresses.country" name="orgaddressescountry" type="text" placeholder="placeholder" class="form-control input-md"/>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="singlebutton">Single Button</label>
<div class="col-md-4">
<button id="singlebutton" name="singlebutton" class="btn btn-primary">Button</button>
</div>
</div>
</fieldset>
</form:form>
弹簧servlet.xml中
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- <context:property-placeholder location="classpath:resources/database.properties"> </context:property-placeholder> -->
<context:property-placeholder location="classpath:resources/database.properties"/>
<!-- It register the beans in context and scan the annotations inside beans and activate them -->
<context:component-scan base-package="com.techvision.studycenter" />
<mvc:resources mapping="/resources/**" location="/resources/mytheme/" />
<!-- This allow for dispatching requests to Controllers by registering
two beans - DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter -->
<mvc:annotation-driven />
<!-- This helps in mapping the logical view names to directly view files under a certain pre-configured directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- This resolves messages from resource bundles for different locales -->
<!-- <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages" />
</bean> -->
<tx:annotation-driven transaction-manager="hibernateTransactionManager"/>
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<property name="driverClassName" value="${database.driver}"></property>
<property name="url" value="${database.url}"></property>
<property name="username" value="${database.user}"></property>
<property name="password" value="${database.password}"></property>
</bean>
<bean class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" id="sessionFactory">
<property name="dataSource" ref="dataSource"></property>
<property name="annotatedClasses">
<list>
<value>com.techvision.studycenter.model.Orgaddress</value>
<value>com.techvision.studycenter.model.Organization</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto} </prop>
</props>
</property>
</bean>
<bean class="org.springframework.orm.hibernate3.HibernateTransactionManager" id="hibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
答案 0 :(得分:0)
放置您的组织模型
public String getPrivacyname()
{
String address1=null;
if (orgaddresses != null )
{
List<Orgaddress> orgAddresses= new ArrayList(getOrgaddresses());
if(orgAddresses!=null)
{
for(Orgaddress OrgAddress : orgAddresses){
address1= OrgAddress.getAddress1();
}
}
}
return address1;
}