2015-05-11 19 views
0

我試圖保存一個包含用戶的實體站點,此用戶在數據庫中註冊,我不想保存它。
問題是,當我嘗試保存的網站,我收到以下錯誤:「沖洗前保存瞬態對象」錯誤

org.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved transient instance - save the transient instance before flushing: com.project.netmg.bo.impl.User; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.project.netmg.bo.impl.User 

我認爲,它嘗試保存用戶太多,但它不是我想要的。我只想搜索好用戶並將其分配給該網站。

java代碼:

public String saveSite(@Valid @ModelAttribute SiteForm siteForm, BindingResult bindingResult, Model uiModel) { 

    populateSiteforSave(siteForm); 
    _siteService.saveSite(siteForm.getSite()); 
    return WebConstants.REDIRECT_TO_VIEW_SITE_URL + siteForm.getSite().getSiteName(); 
} 

private void populateSiteforSave(SiteForm siteForm) { 
    siteForm.getSite().setCountry((Country) _countryService.getCountryByName(siteForm.getSite().getCountry().getName())); 
    siteForm.getSite().setBusiness((Business) _businessService.getBusinessById(siteForm.getSite().getBusiness().getId())); 
    siteForm.getSite().setStatus((Status) _statusService.getStatusById(siteForm.getSite().getStatus().getId())); 
    if (!siteForm.getLocalItFullName().isEmpty()) { 
     siteForm.getSite().setLocalIt(_userService.findUserByUserFullName(siteForm.getLocalItFullName())); // user 
    } else { 
     siteForm.getSite().setLocalIt(null); 
    } 
    if (!siteForm.getRifFullName().isEmpty()) { 
     siteForm.getSite().setRif(_userService.findUserByUserFullName(siteForm.getRifFullName())); //user 
    } else { 
     siteForm.getSite().setRif(null); 
    } 
    if (siteForm.getSite().getLocalContact().getId() != null) { 
     siteForm.getSite().setLocalContact((User) _userService.findUserByUsername(siteForm.getSite().getLocalContact().getUsername())); //user 
    } 
} 

站點類:

@Entity 
@Table(name = "SITE", uniqueConstraints = { @UniqueConstraint(columnNames = { "SITE_COUNTRY_ID", "SITE_NAME" }) }) 
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED, withModifiedFlag = true) 
public class Site implements ISite { 

/** The Constant serialVersionUID. */ 
private static final long serialVersionUID = -390717603276436784L; 

/** The id. */ 
@Id 
@GeneratedValue(strategy = GenerationType.AUTO) 
@Column(name = "SITE_ID", unique = true, nullable = false) 
private long id; 

/** The site address. */ 
@Column(name = "SITE_ADDRESS", length = BusinessConstants.SITE_ADDRESS) 
private String address; 

/** The site analog phone number. */ 
@Column(name = "SITE_ANALOG_PHONE_NUMBER", length = BusinessConstants.SITE_ANALOG_PHONE_NUMBER) 
private String analogPhoneNumber; 

/** The site comment. */ 
@Column(name = "SITE_COMMENT", length = BusinessConstants.SITE_COMMENT) 
private String comment; 

/** The site entity code. */ 
@Digits(integer = 3, fraction = 0, message = "Please enter max 3 digits") 
@Column(name = "SITE_ENTITY_CODE", nullable = false) 
private long entityCode; 

/** The site invoice code. */ 
@Digits(integer = 10, fraction = 0, message = "Please enter max 10 digits") 
@Column(name = "SITE_INVOICE_CODE", nullable = false) 
private long invoiceCode; 

/** The site local it phone. */ 
@Column(name = "SITE_LOCAL_IT_PHONE", length = BusinessConstants.SITE_LOCAL_IT_PHONE) 
private String localItPhone; 

/** The site name. */ 
@NotBlank 
@Column(name = "SITE_NAME", nullable = false, length = BusinessConstants.SITE_NAME) 
private String siteName; 

/** The site subnet. */ 
@NotBlank 
@Column(name = "SITE_SUBNET", nullable = false, length = BusinessConstants.SITE_SUBNET) 
private String subnet; 

/** The site user number. */ 
@Digits(integer = 4, fraction = 0, message = "Please enter max 4 digits") 
@Column(name = "SITE_USER_NUMBER") 
private Long userNumber; 

/** The business. */ 
@Valid 
@ManyToOne(fetch = FetchType.LAZY) 
@JoinColumn(name = "SITE_BUSINESS_ID", nullable = false) 
private Business business; 

/** The country. */ 
@Valid 
@ManyToOne(fetch = FetchType.EAGER) 
@JoinColumn(name = "SITE_COUNTRY_ID") 
private Country country; 

/** The local contact. */ 
@ManyToOne(fetch = FetchType.LAZY) 
@JoinColumn(name = "SITE_LOCAL_CONTACT", nullable = true) 
private User localContact; 

/** The local it. */ 
@ManyToOne(fetch = FetchType.LAZY) 
@JoinColumn(name = "SITE_LOCAL_IT", nullable = true) 
private User localIt; 

/** The rif. */ 
@ManyToOne(fetch = FetchType.LAZY) 
@JoinColumn(name = "SITE_RIF", nullable = true) 
private User rif; 

/** The status. */ 
@Valid 
@ManyToOne(fetch = FetchType.LAZY) 
@JoinColumn(name = "SITE_STATUS_ID", nullable = false) 
private Status status; 

/** The end date. */ 
@Column(name = "SITE_END_DATE") 
@Temporal(TemporalType.TIMESTAMP) 
@DateTimeFormat(iso = ISO.DATE_TIME) 
private Date endDate = null; 

@ManyToMany(targetEntity = User.class, mappedBy = "userSites", fetch = FetchType.LAZY) 
@NotAudited 
private Set<IUser> siteUsers; 

用戶等級:

@Entity 
@Table(name = "USERS") 
public class User implements IUser { 

/** The Constant serialVersionUID. */ 
private static final long serialVersionUID = 6741623705511494367L; 
private static final String USER_ID = "USER_ID"; 

/** The user id. */ 
@Id 
@GeneratedValue(strategy = GenerationType.AUTO) 
@Column(name = USER_ID) 
private Long id; 

/** The user first name. */ 
@Column(name = "FIRSTNAME", length = BusinessConstants.USER_FIRSTNAME, nullable = false) 
@NotNull 
private String userFirstName; 

/** The user last name. */ 
@Column(name = "LASTNAME", length = BusinessConstants.USER_LASTNAME, nullable = false) 
@NotNull 
private String userLastName; 

/** The user email. */ 
@Column(name = "EMAIL", length = BusinessConstants.USER_EMAIL) 
@NotNull 
private String userEmail; 

/** The user uid. */ 
@Column(name = "LOGIN", length = BusinessConstants.USER_LOGIN, nullable = false, unique = true) 
@NotNull 
private String username; 

@Valid 
@ManyToOne(fetch = FetchType.LAZY) 
@JoinColumn(name = "PROFILE_ID", referencedColumnName = "PROF_ID", nullable = false) 
private Profile profile; 

@BatchSize(size = 20) 
@ManyToMany(cascade = { CascadeType.MERGE, CascadeType.PERSIST }, fetch = FetchType.LAZY,targetEntity=Site.class) 
@JoinTable(name = "USER_SITE", 
joinColumns = { @JoinColumn(name = USER_ID, nullable = false) }, 
inverseJoinColumns = { @JoinColumn(name = "SITE_ID", nullable = false) }) 
private Set<ISite> userSites; 

@ManyToMany(cascade = { CascadeType.MERGE, CascadeType.PERSIST }, fetch = FetchType.LAZY,targetEntity=Region.class) 
@JoinTable(name = "USER_REGION", 
joinColumns = { @JoinColumn(name = USER_ID, nullable = false) }, 
inverseJoinColumns = { @JoinColumn(name = "REGION_ID", nullable = false) }) 
private Set<IRegion> userRegions; 

@BatchSize(size = 20) 
@ManyToMany(cascade = { CascadeType.MERGE, CascadeType.PERSIST }, fetch = FetchType.LAZY,targetEntity=Zone.class) 
@JoinTable(name = "USER_ZONE", 
joinColumns = { @JoinColumn(name = USER_ID, nullable = false) }, 
inverseJoinColumns = { @JoinColumn(name = "ZONE_ID", nullable = false) }) 
private Set<IZone> userZones; 

@BatchSize(size = 20) 
@ManyToMany(cascade = { CascadeType.MERGE, CascadeType.PERSIST }, fetch = FetchType.LAZY,targetEntity=Country.class) 
@JoinTable(name = "USER_COUNTRY", 
joinColumns = { @JoinColumn(name = USER_ID, nullable = false) }, 
inverseJoinColumns = { @JoinColumn(name = "COUNTRY_ID", nullable = false) }) 
private Set<ICountry> userCountries; 

@Transient 
private Collection<? extends GrantedAuthority> authorities; 

@Transient 
private String userFullName; 
+0

@SteveChaloner我嘗試過,但這個是救子對象,但我並不想救孩子,我只是想關聯。當我做cascade.all我得到一個錯誤,說我的對象用戶包含一些缺少的字段 –

+0

看到這個答案到上面的帖子http://stackoverflow.com/a/20286061/1063929 –

回答

1

你不能真正擁有它。如果對象圖包含User,那麼如果在代碼中進行了更改,它將(必須)持久化。您是否考慮過平均值取Site(包括User),然後更改localContact並再次保留Site

如果你想在localContact是在對象圖可設置的,但不堅持的話,就可以用@Transient註釋:

/** The local contact. */ 
@Transient 
private User localContact; 

希望有所幫助。

乾杯,

+0

編輯帖子感謝您的幫助,它的工作,但我不明白爲什麼localContact造成的錯誤。你能解釋一下你的意思嗎? –

+0

最有可能在'localContact'上設置的'User'沒有與DB中現有用戶相對應的ID,並且由於您沒有明確設置另一個答案中提到的級聯選項,所以您必須明確地持久化首先是用戶。 –

+0

好的明白謝謝 –

0

你有沒有標註級聯= CascadeType.ALL你的實體對象映射

例如:

 @ManyToOne(cascade = CascadeType.ALL) 
     private String entityType; 
+0

是的,但這是爲了拯救孩子們實體沒有?我不想將它保存到數據庫中,我只是想分配它。當我做cascade.all我得到一個錯誤在我的領域註釋@NotNull –

+0

一個更重要的細節,問題是隻是當一個用戶null。你有好主意嗎 ? –

+0

你能展示你的實體類是怎麼樣的。 – Thomas

相關問題