Hello everyone, good afternoon!
Well, briefly I have a problem where I have no idea what might be happening. In my opinion, such behavior does not make sense.
I'm using Spring MVC + Spring Data to facilitate CRUD operations on my system. I have a page in which I make a category record, it is simple, just a "name" field and another to check if the category is active.
Next I have a "Destination Management", which aims to register new destinations. In it I have an association with category in which a dropdown list component is populated. Such relationship is one-way OneToOne factor.
The component is normally populated, below its code:
View:
<f:select path="categories.idCategory">
<f:option value="-1" label="-- Selecione uma categoria para associação --"/>
<f:options items="${categoryDropDown}" itemValue="idCategory" itemLabel="ctName"/>
</f:select>
Controller:
//Entra na tela de cadastro de um novo destino
@RequestMapping("newDestination")
public ModelAndView setnewDestination(Model model, HttpServletRequest req){
//Inicializa o Componente DropDown de Categoria
List<Category> category = dashboardFacade.getCategoryList();
List<Category> activeCategory = new ArrayList<Category>();
//Verificação de categorias que não estão ativas
for (Category allCAtegory : category) {
if(allCAtegory.getCtActive() == 1){
activeCategory.add(allCAtegory);
}
}
model.addAttribute("categoryDropDown", activeCategory);
return new ModelAndView("destination/newDestination", "command", new Destination());
}
Facade:
public List<Category> getCategoryList(){
return categoryRepository.findAll();
}
Repository:
@Repository
public interface CategoryRepository extends BaseRepository<Category, Long>{
List<Category> findAll();
}
Well, what happens in reality is that when I go to register a new destination and select a category the same and saved successfully in the database. But when I have again register a destination my List<Category> category = dashboardFacade.getCategoryList();
comes with inconsistent object. That is, with null values.
Category object populating the dropbox without its association being saved:
- [idCategory = 1, ctActive = 1, ctName = National, tenantId = 2],
- [idCategory = 2, ctActive = 0, ctName = International, tenantId = 2],
- [idCategory = 3, ctActive = 1, ctName = Cruise, tenantId = 2],
- [idCategory = 4, ctActive = 1, ctName = Spatial, tenantId = 2],
I saved a new destination with the category name "National". When the dropdown it just does not appear. This is the status that category is ignored:
- [idCategory = 1, ctActive = 0, ctName = null, tenantId = null],
- [idCategory = 2, ctActive = 0, ctName = International, tenantId = 2],
- [idCategory = 3, ctActive = 1, ctName = Cruise, tenantId = 2],
- [idCategory = 4, ctActive = 1, ctName = Spatial, tenantId = 2],
I have no idea what might be happening.
If anyone can help, I am very grateful.
Hugs to all
EDITION
Below are the mappings:
Destination.java
package br.com.joocebox.model;
import java.io.Serializable;
import javax.persistence.*;
import org.eclipse.persistence.annotations.Multitenant;
import org.eclipse.persistence.annotations.TenantDiscriminatorColumn;
import org.eclipse.persistence.config.PersistenceUnitProperties;
/**
* The persistent class for the destination database table.
*
*/
@Entity
@Table(name="destination")
@Multitenant
@TenantDiscriminatorColumn(name="tenant_id", discriminatorType=DiscriminatorType.INTEGER, contextProperty=PersistenceUnitProperties.MULTITENANT_PROPERTY_DEFAULT)
@NamedQuery(name="Destination.findAll", query="SELECT d FROM Destination d")
public class Destination implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id_destination")
private Long idDestination;
@Column(name="tenant_id", insertable=false, updatable=false)
private Long tenantId;
@Column(name="active")
private byte dtActive;
@Column(name="appear_website")
private Boolean dtAppearWebsite;
@Lob
@Column(name="description")
private String dtDescription;
@Column(name="highlight_website")
private Boolean dtHighlightWebsite;
@Column(name="name")
private String dtName;
@OneToOne(cascade=CascadeType.PERSIST)
@JoinColumn(name="fk_streetview")
private StreetView streetView;
@OneToOne(cascade=CascadeType.PERSIST)
@JoinColumn(name="fk_video")
private Video video;
@OneToOne
@JoinColumn(name="fk_category")
private Category categories;
// //bi-directional many-to-one association to Agency
// @ManyToOne(fetch=FetchType.LAZY)
// @JoinColumn(name="fk_agency")
// private Agency agency;
//Profiles of System. These profiles are all enum type.
@OneToOne(cascade=CascadeType.PERSIST)
@JoinColumn(name="fk_economic")
private EconomicProfile economicProfiles;
@OneToOne(cascade=CascadeType.PERSIST)
@JoinColumn(name="fk_general")
private GeneralProfile generalProfiles;
@OneToOne(cascade=CascadeType.PERSIST)
@JoinColumn(name="fk_social")
private SocialProfile socialProfiles;
@OneToOne(cascade=CascadeType.PERSIST)
@JoinColumn(name="fk_trip")
private TripProfile tripProfiles;
@OneToOne(cascade=CascadeType.PERSIST)
@JoinColumn(name="fk_weather")
private WeatherProfile weatherprofile;
//End of Profiles of System.
// @OneToMany(mappedBy="destination")
// private Set<Image> images;
public Destination() {
}
public Long getIdDestination() {
return this.idDestination;
}
//Getter and Setters
}
Category.java
package br.com.joocebox.model;
import java.io.Serializable;
import java.util.Set;
import javax.persistence.*;
import org.eclipse.persistence.annotations.Multitenant;
import org.eclipse.persistence.annotations.TenantDiscriminatorColumn;
import org.eclipse.persistence.config.PersistenceUnitProperties;
/**
* The persistent class for the category database table.
*
*/
@Entity
@Table(name="category")
@Multitenant
@TenantDiscriminatorColumn(name="tenant_id", discriminatorType=DiscriminatorType.INTEGER, contextProperty=PersistenceUnitProperties.MULTITENANT_PROPERTY_DEFAULT)
@NamedQuery(name="Category.findAll", query="SELECT c FROM Category c")
public class Category implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id_category")
private Long idCategory;
@Column(name="ct_active")
private int ctActive;
@Column(name="ct_name")
private String ctName;
@Column(name="tenant_id", insertable=false, updatable=false)
private Long tenantId;
public Category() {
}
public Long getIdCategory() {
return this.idCategory;
}
//Getters and Setters
@Override
public String toString() {
return "Category [idCategory=" + idCategory + ", ctActive=" + ctActive
+ ", ctName=" + ctName + ", tenantId=" + tenantId + "]";
}
}
Thank you!
Filipeportes,
It did not work. I really do not know what it can be.
I did it this way:
Category.java:
@Entity
@Table(name="category")
@Multitenant
@TenantDiscriminatorColumn(name="tenant_id", discriminatorType=DiscriminatorType.INTEGER, contextProperty=PersistenceUnitProperties.MULTITENANT_PROPERTY_DEFAULT)
@NamedQuery(name="Category.findAll", query="SELECT c FROM Category c")
public class Category implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id_category")
private Long idCategory;
@Column(name="ct_active")
private int ctActive;
@Column(name="ct_name")
private String ctName;
@Column(name="tenant_id", insertable=false, updatable=false)
private Long tenantId;
//Relacionamento com Category
@OneToMany(mappedBy="categories", fetch=FetchType.LAZY)
private Set<Destination> destination;
//Getters and Setters
Destination.java:
@Entity
@Table(name="destination")
@Multitenant
@TenantDiscriminatorColumn(name="tenant_id", discriminatorType=DiscriminatorType.INTEGER, contextProperty=PersistenceUnitProperties.MULTITENANT_PROPERTY_DEFAULT)
@NamedQuery(name="Destination.findAll", query="SELECT d FROM Destination d")
public class Destination implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id_destination")
private Long idDestination;
@Column(name="tenant_id", insertable=false, updatable=false)
private Long tenantId;
@Column(name="active")
private byte dtActive;
@Column(name="appear_website")
private Boolean dtAppearWebsite;
@Lob
@Column(name="description")
private String dtDescription;
@Column(name="highlight_website")
private Boolean dtHighlightWebsite;
@Column(name="name")
private String dtName;
@OneToOne(cascade=CascadeType.PERSIST)
@JoinColumn(name="fk_streetview")
private StreetView streetView;
@OneToOne(cascade=CascadeType.PERSIST)
@JoinColumn(name="fk_video")
private Video video;
@ManyToOne(cascade=CascadeType.REFRESH)
@JoinColumn(name="fk_category")
private Category categories;
//Getters and Setters
It seems like something is being cached, because when I shutdown and upload the server again my object comes in consistently.
Embrace