我有一個簡單的Hibernate POJO粘貼在下面(爲簡潔起見刪除了構造函數和setter)。我的問題出現在「用戶」關係中。 Hibernate延遲加載關係很好,但是當我的CRUD webservice調用(也在下面)編組這個對象的實例時,它調用關係的「get」方法,因此在Hibernate中拋出「No transaction」異常,因爲JAXB沒有訪問會話或事務內部的關係。JAXB,休眠,延遲加載
POJO:
@Entity
@Table(name = "ldapservers", uniqueConstraints = @UniqueConstraint(columnNames = "hostname"))
@XmlRootElement(name = "ldap-server")
@SuppressWarnings("unused")
public class LdapServer implements Serializable
{
private int ldapServerId;
private String hostname;
private int port;
private Date createDate;
private String createUser;
private Set<User> users = new HashSet<User>(0);
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "ldapServerID", unique = true, nullable = false)
@XmlAttribute(name="id")
public int getLdapServerId()
{
return this.ldapServerId;
}
@Column(name = "hostname", unique = true, nullable = false)
@XmlElement
public String getHostname()
{
return this.hostname;
}
@Column(name = "port", nullable = false)
@XmlElement
public int getPort()
{
return this.port;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "createDate", nullable = false, length = 19)
@XmlAttribute(name="create-date")
public Date getCreateDate()
{
return this.createDate;
}
@Column(name = "createUser", nullable = false)
@XmlAttribute(name="create-user")
public String getCreateUser()
{
return this.createUser;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "ldapServer")
public Set<User> getUsers()
{
return this.users;
}
}
Web服務調用:
@GET
@Path("/fetch/{id}")
@Produces("application/xml")
public LdapServer getLdapServer(@PathParam("id") int ldapServerID)
{
logger.debug("Fetching LdapServer ID "+ldapServerID);
LdapServer ls = this.home.findById(ldapServerID);
if (ls!=null)
{
logger.debug("Found LdapServer ID "+ldapServerID);
}
else
{
logger.debug("LdapServer ID "+ldapServerID+" not found.");
}
return ls;
}
因爲內RestEasy的內外此調用發生錯誤,表明期間發生的問題,我還沒有包括DAO/EJB代碼編組。