I want to create a Generic class which implements AttributeConverter and can be used in #Converter annotation for all enums.
I have two different enums each one has a converter class but both converter classes are doing same thing.
The following are the code Snippet.
public enum Type {
BASIC(1, "Basic"),
ADVANCE(2, "Advance");
private final Integer id;
private final String name;
private Type(Integer id, String name) {
this.id = id;
this.name = name;
}
public static Type getType(Integer id) {
if (id == null) { return null; }
for (Type type: Type.values()) {
if (id == type.getId()) {
return type;
}
}
return null;
}
public Integer getId() { return id; }
public String getName() { return name; }
}
#Converter(autoApply = true)
public class TypeConverter implements AttributeConverter<Type, Integer> {
#Override
public Integer convertToDatabaseColumn(Type type) {
return type.getId();
}
#Override
public Type convertToEntityAttribute(Integer databaseValue) {
return Type.getType(databaseValue);
}
}
public enum State {
FIRST(1, "First"),
SECOND(2, "Second");
private final Integer id;
private final String name;
private State(Integer id, String name) {
this.id = id;
this.name = name;
}
public static State getState(Integer id) {
if (id == null) { return null; }
for (State state: State.values()) {
if (id == state.getId()) {
return state;
}
}
return null;
}
public Integer getId() { return id; }
public String getName() { return name; }
}
#Converter(autoApply = true)
public class StateConverter implements AttributeConverter<State, Integer> {
#Override
public Integer convertToDatabaseColumn(State state) {
return state.getId();
}
#Override
public State convertToEntityAttribute(Integer databaseValue) {
return State.getState(databaseValue);
}
}
#Table(name = "um_user")
public class User
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String username;
private String password
#Convert(converter = TypeConverter.class)
private Type type;
#Convert(converter = StateConverter.class)
private State state;
}
Now I want to create a generic converter that can be used instead of both TypeConverter & StateConverter.
Related
I am trying to fetch list of train entities using source and destination properties of route entity which has one to many relationship with each other. The train table having route id as foreign key. The table names are route and train respectively. Please help me as the query is throwing java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List com.infyrail.app.repository.RouteRepository.findBySourceDestination(java.lang.String,java.lang.String)!
RouteRepository:
public interface RouteRepository extends JpaRepository<RouteEntity, Integer> {
#Query("SELECT t FROM train t JOIN route r WHERE r.source=?1 AND r.destination=?2")
public List<TrainEntity> findBySourceDestination(String source,String destination);
}
RouteEntity:
#Entity
#Table(name="route")
public class RouteEntity {
#Id
#GenericGenerator(name="route_id",
strategy="com.infyrail.app.generator.RouteIdGenerator")
#GeneratedValue(generator = "route_id")
#Min(value = 100)
#Max(value = 999)
Integer id;
String source;
String destination;
#OneToMany(mappedBy = "route",
cascade=CascadeType.ALL,orphanRemoval = true)
private List<TrainEntity> trainList;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getDestination() {
return destination;
}
public void setDestination(String destination) {
this.destination = destination;
}
public List<TrainEntity> getTrainList() {
return trainList;
}
public void setTrainList(List<TrainEntity> trainList) {
this.trainList = trainList;
}
}
TrainEntity:
#Entity
#Table(name="train")
public class TrainEntity {
//#GeneratedValue(strategy = GenerationType.IDENTITY)
#Id
#GenericGenerator(name="train_id",
strategy="com.infyrail.app.generator.TrainIdGenerator")
#GeneratedValue(generator = "train_id")
#Min(value = 100)
#Max(value = 999)
Integer id;
String trainName;
String arrivalTime;
String departureTime;
Double fare;
#ManyToOne(fetch = FetchType.LAZY)
#Autowired
RouteEntity route;
public RouteEntity getRoute() {
return route;
}
public void setRoute(RouteEntity route) {
this.route = route;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTrainName() {
return trainName;
}
public void setTrainName(String trainName) {
this.trainName = trainName;
}
public String getArrivalTime() {
return arrivalTime;
}
public void setArrivalTime(String arrivalTime) {
this.arrivalTime = arrivalTime;
}
public String getDepartureTime() {
return departureTime;
}
public void setDepartureTime(String departureTime) {
this.departureTime = departureTime;
}
public Double getFare() {
return fare;
}
public void setFare(Double fare) {
this.fare = fare;
}
}
Here is the problem. Your query should define the table bean name instead of the actual table name.
In your case you should use TrainEntity instead of train and RouteEntity instead of route.
public interface RouteRepository extends JpaRepository<RouteEntity, Integer> {
#Query("SELECT t FROM TrainEntity t JOIN RouteEntity r WHERE r.source=?1 AND r.destination=?2")
public List<TrainEntity> findBySourceDestination(String source,String destination);
}
MenuModel
#Entity
#Table(name="M_MENU", uniqueConstraints={#UniqueConstraint(columnNames={"NAME"})})
public class MenuModel {
private Integer id;
private String code;
private String name;
private String controller;
private Integer parent_id;
#Id
#Column(name="ID")
#GeneratedValue(strategy=GenerationType.TABLE, generator="M_MENU")
#TableGenerator(name="M_MENU", table="M_SEQUENCE",
pkColumnName="SEQUENCE_NAME", pkColumnValue="M_MENU_ID",
valueColumnName="SEQUENCE_VALUE", allocationSize=1, initialValue=0
)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
#Column(name="CODE")
public String getCode() {
return code;
}
public void setCode(String kode) {
this.code = kode;
}
#Column(name="NAME")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#Column(name="CONTROLLER")
public String getController() {
return controller;
}
public void setController(String controller) {
this.controller = controller;
}
#Column(name="PARENT_ID")
public Integer getParent_id() {
return parent_id;
}
public void setParent_id(Integer parent_id) {
this.parent_id = parent_id;
}
}
UserAccessModel
#Entity
#Table(name="M_USER_ACCESS")
public class UserAccessModel {
private Integer id;
//join table role
private Integer idRole;
private RoleModel roleModel;
//join table menu
private Integer idMenu;
private MenuModel menuModel;
#Id
#Column(name="ID")
#GeneratedValue(strategy=GenerationType.TABLE, generator="M_USER_ACCESS")
#TableGenerator(name="M_USER_ACCESS", table="M_SEQUENCE",
pkColumnName="SEQUENCE_NAME", pkColumnValue="M_USER_ACCESS_ID",
valueColumnName="SEQUENCE_VALUE", allocationSize=1, initialValue=0
)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
#Column(name="ID_ROLE")
public Integer getIdRole() {
return idRole;
}
public void setIdRole(Integer idRole) {
this.idRole = idRole;
}
#ManyToOne
#JoinColumn(name="ID_ROLE", nullable=true, updatable=false, insertable=false)
public RoleModel getRoleModel() {
return roleModel;
}
public void setRoleModel(RoleModel roleModel) {
this.roleModel = roleModel;
}
#Column(name="ID_MENU")
public Integer getIdMenu() {
return idMenu;
}
public void setIdMenu(Integer idMenu) {
this.idMenu = idMenu;
}
#ManyToOne
#JoinColumn(name="ID_MENU", nullable=true, updatable=false, insertable=false)
public MenuModel getMenuModel() {
return menuModel;
}
public void setMenuModel(MenuModel menuModel) {
this.menuModel = menuModel;
}
}
MenuDaoImpl
#Override
public List<MenuModel> searchByRole(Integer idRole) {
// TODO Auto-generated method stub
Session session = this.sessionFactory.getCurrentSession();
List<MenuModel> menuModelListRole = new ArrayList<MenuModel>();
Criteria userAccessCriteria = session.createCriteria(UserAccessModel.class,"UA");
Criteria menuCriteria = userAccessCriteria.createCriteria("menuModel","M");
userAccessCriteria.add(Restrictions.eq("idRole", ""+idRole+""));
ProjectionList properties = Projections.projectionList();
properties.add(Projections.property("M.id"));
properties.add(Projections.property("M.name"));
properties.add(Projections.property("M.code"));
properties.add(Projections.property("M.controller"));
properties.add(Projections.property("M.parent_id"));
menuCriteria.setProjection(properties);
menuModelListRole = menuCriteria.list();
return menuModelListRole;
}
I want to get result of the following sql:
select M.ID ID, M.NAME NAME, M.CODE CODE, M.CONTROLLER CONTROLLER,
M.PARENT_ID PARENT from M_MENU M join M_USER_ACCESS UA on UA.ID_MENU
= M.ID where UA.ID_ROLE="+idRole+"
I got error in method searchByRole in menuModelListRole = menuCriteria.list();. How can i resolve the problem?
Here private Integer idRole; is of Integer type, but you are passing idRole as String in Restrictions.eq("idRole", ""+idRole+""). SO you are getting java.lang.String cannot be cast to java.lang.Integer in Spring
Changing you restriction value from Restrictions.eq("idRole", ""+idRole+"") to Restrictions.eq("idRole", idRole) should solve your problem.
I have entity as follow.
#Entity
#Table(name = "BankProduct")
public class Product {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
#ManyToOne
private ProductUseType type;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
#ManyToOne
private ProductSerial serial;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ProductUseType getType() {
return type;
}
public void setType(ProductUseType type) {
this.type = type;
}
public ProductSerial getSerial() {
return serial;
}
public void setSerial(ProductSerial serial) {
this.serial = serial;
}
}
My controller is :
#RestController
public class DEmoController {
#Autowired
private ProductRepository productRepository;
#GetMapping("/products")
public Returns products() {
return new Returns(ReturnStatus.SUCCESS.getStatus(), productRepository.findAll(), null);
}
}
It will load both of type and serial of product.
Can I only load type but not to load serial?
I don't want to add fetch=FetchType.LAZY to serial, because if next time I want to load serial but not to load type, it will be terrible.
Check the Projection interface
Create a interface ProductProjection
interface ProductProjection {
String getName();
String getType();
}
and add a method in you Repository
List<ProductProjection> findAllProjection()
That's the whole point of fetch=FetchType.LAZY. It'll not load any of your types/fields until you ask for them explicitly.
Take a look at this question: Link
I have three tables :
1. org,
2. product_info
3. service_info.
And, table product_info is mapping table service_info ManyToMany,
means,many products mapping many services.
While,table org is mapping table product_info OneToMany,
means,one org have many products.
When I initialize my web
I want to view the org table's column. How to do ?
Under classes are the persistent classes for three tables.
ProductService class:
`
#Entity
#Table(name="product_service")
#Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class ProductService implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private String id;
private ServiceInfo serviceInfo;//this is the service table
private String parammapping;
private ProductInfo productInfo;//this is the product table
// Constructors
/** default constructor */
public ProductService() {
}
/** minimal constructor */
public ProductService(String id) {
this.id = id;
}
// Property accessors
#Id
#Column(name="ID", unique=true, nullable=false, length=50)
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
#ManyToOne(fetch=FetchType.LAZY)
#JoinColumn(name="SERVICEID")
public ServiceInfo getServiceInfo() {
return this.serviceInfo;
}
public void setServiceInfo(ServiceInfo serviceInfo) {
this.serviceInfo = serviceInfo;
}
#Column(name="PARAMMAPPING", length=1000)
public String getParammapping() {
return parammapping;
}
public void setParammapping(String parammapping) {
this.parammapping = parammapping;
}
#ManyToOne(fetch=FetchType.LAZY)
#JoinColumn(name="PRODUCTID")
public ProductInfo getProductInfo() {
return this.productInfo;
}
public void setProductInfo(ProductInfo productInfo) {
this.productInfo = productInfo;
}
}`
baseOrg class:
#Entity
#Table(name="base_org")
#Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class BaseOrg implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private String id;
private String code;
private String name;
private List<BaseRuleEngineLog> serviceUsedLogs = new ArrayList<BaseRuleEngineLog>(0);
private List<ProductInfo> productInfos = new ArrayList<ProductInfo>(0);
private List<BaseCreditQuery> baseCreditQueries = new ArrayList<BaseCreditQuery>(0);
// Constructors
/** default constructor */
public BaseOrg() {
}
/** minimal constructor */
public BaseOrg(String id) {
this.id = id;
}
#Id
#Column(name="ID", unique=true, nullable=false, length=50)
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
#Column(name="CODE", length=50)
public String getCode() {
return this.code;
}
public void setCode(String code) {
this.code = code;
}
#Column(name="NAME", length=200)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
#OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="baseOrg")
public List<BaseRuleEngineLog> getServiceUsedLogs() {
return this.serviceUsedLogs;
}
public void setServiceUsedLogs(List<BaseRuleEngineLog> serviceUsedLogs) {
this.serviceUsedLogs = serviceUsedLogs;
}
#OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="baseOrg")
public List<ProductInfo> getProductInfos() {
return this.productInfos;
}
public void setProductInfos(List<ProductInfo> productInfos) {
this.productInfos = productInfos;
}
#OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="baseOrg")
public List<BaseCreditQuery> getBaseCreditQueries() {
return this.baseCreditQueries;
}
public void setBaseCreditQueries(List<BaseCreditQuery> baseCreditQueries) {
this.baseCreditQueries = baseCreditQueries;
}
}
productInfo class:
#Entity
#Table(name="product_info")
#Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class ProductInfo implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private String id;
private BaseOrg baseOrg;//baseOrg table
private String code;
private String name;
private String orgcode;
private List<ProductService> productServices = new ArrayList<ProductService>(0);
// Constructors
/** default constructor */
public ProductInfo() {
}
/** minimal constructor */
public ProductInfo(String id) {
this.id = id;
}
// Property accessors
#Id
#Column(name="ID", unique=true, nullable=false, length=50)
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
#ManyToOne(fetch=FetchType.LAZY)
#JoinColumn(name="ORGID")
public BaseOrg getBaseOrg() {
return this.baseOrg;
}
public void setBaseOrg(BaseOrg baseOrg) {
this.baseOrg = baseOrg;
}
#Column(name="CODE", length=100)
public String getCode() {
return this.code;
}
public void setCode(String code) {
this.code = code;
}
#Column(name="NAME", length=100)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
#Column(name="ORGCODE", length=100)
public String getOrgcode() {
return this.orgcode;
}
public void setOrgcode(String orgcode) {
this.orgcode = orgcode;
}
#OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="productInfo")
public List<ProductService> getProductServices() {
return this.productServices;
}
public void setProductServices(List<ProductService> productServices) {
this.productServices = productServices;
}
}
serviceInfo class
#Entity
#Table(name="service_info")
#Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class ServiceInfo implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private String id;
private String name;
private String code;
private List<ProductService> productServices = new ArrayList<ProductService>(0);
// Constructors
/** default constructor */
public ServiceInfo() {
}
/** minimal constructor */
public ServiceInfo(String id) {
this.id = id;
}
// Property accessors
#Id
#Column(name="ID", unique=true, nullable=false, length=50)
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
#Column(name="NAME", length=100)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
#Column(name="CODE", length=100)
public String getCode() {
return this.code;
}
public void setCode(String code) {
this.code = code;
}
#OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="serviceInfo")
public List<ProductService> getProductServices() {
return this.productServices;
}
public void setProductServices(List<ProductService> productServices) {
this.productServices = productServices;
}
}
product_service table
Thank you for forgiving my poor English,this is my first time questioning on Stack Overflow.
OK,I resolve it.
i found its so sample,lol.
Because,most criteria have been packaged.At the first time ,I want to use the HQL to resolve it,but in vain.I just add this sentences,and getted it.
enter image description here
I'm trying to implement One-to-Many relation between two tables using hibernate. Here is my code:
#Entity
public class Board
{
#Id
#Column(name = "board_id")
#GeneratedValue
private long id;
#Column
private String owner;
#Column
private String title;
#Column
private String refresh;
#Column
private Timestamp createDate;
#Column
private Timestamp modifyDate;
#OneToMany(mappedBy="board", cascade=CascadeType.ALL)
private List<Item> items;
public long getId()
{
return id;
}
public void setId(long id)
{
this.id = id;
}
public String getOwner()
{
return owner;
}
public void setOwner(String owner)
{
this.owner = owner;
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public String getRefresh()
{
return refresh;
}
public void setRefresh(String refresh)
{
this.refresh = refresh;
}
public Timestamp getCreateDate()
{
return createDate;
}
public void setCreateDate(Timestamp createDate)
{
this.createDate = createDate;
}
public Timestamp getModifyDate()
{
return modifyDate;
}
public void setModifyDate(Timestamp modifyDate)
{
this.modifyDate = modifyDate;
}
public List<Item> getItems()
{
return items;
}
public void setItems(List<Item> items)
{
this.items = items;
}
}
and second table:
#Entity
public class Item
{
public enum Type
{
link,
image,
presentation;
}
public enum JavaScript
{
enable,
disable;
}
#Id
#Column
#GeneratedValue
private long id;
#ManyToOne(fetch = FetchType.EAGER)
#JoinColumn(name = "board_id")
private Board board;
#Column
private Type type;
#Column(length = 10000)
private String link;
#Column
private String image;
#Column
private String presentation;
#Column
private String time;
#Column
private JavaScript javaScript;
#Column
private String first;
#Column
private String last;
#Transient
private MultipartFile imageFile;
#Transient
private MultipartFile presentationFile;
public long getId()
{
return id;
}
public void setId(long id)
{
this.id = id;
}
public Board getBoard()
{
return board;
}
public void setBoard(Board board)
{
this.board = board;
}
public Type getType()
{
return type;
}
public void setType(Type type)
{
this.type = type;
}
public String getLink()
{
return link;
}
public void setLink(String link)
{
this.link = link;
}
public String getImage()
{
return image;
}
public void setImage(String image)
{
this.image = image;
}
public String getPresentation()
{
return presentation;
}
public void setPresentation(String presentation)
{
this.presentation = presentation;
}
public String getTime()
{
return time;
}
public void setTime(String time)
{
this.time = time;
}
public JavaScript getJavaScript()
{
return javaScript;
}
public void setJavaScript(JavaScript javaScript)
{
this.javaScript = javaScript;
}
public String getFirst()
{
return first;
}
public void setFirst(String first)
{
this.first = first;
}
public String getLast()
{
return last;
}
public void setLast(String last)
{
this.last = last;
}
public MultipartFile getImageFile()
{
return imageFile;
}
public void setImageFile(MultipartFile imageFile)
{
this.imageFile = imageFile;
}
public MultipartFile getPresentationFile()
{
return presentationFile;
}
public void setPresentationFile(MultipartFile presentationFile)
{
this.presentationFile = presentationFile;
}
}
but I can't get it working. board_id is always null in item table. Hibernate output looks strange:
Hibernate: insert into Board (board_id, createDate, modifyDate, owner, refresh, title) values (null, ?, ?, ?, ?, ?)
Hibernate: insert into Item (id, board_id, first, image, javaScript, last, link, presentation, time, type) values (null, ?, ?, ?, ?, ?, ?, ?, ?, ?)
any ideas?
To expand on the comment by #Antoniossss, you need to set the relation on both sides before persisting.
// Create owning entity first
Board board = new Board();
// Create child entities
Item item1 = new Item();
item1.setBoard(board); // set relation on Item side
board.getItems().add(item1); // set relation on Board side
Also note that it is considered good practice to initialize collection fields immediately, so that Board#getItems() never returns null.
Quick tip:
When using #GeneratedValue for an #Id field, it's best to avoid explicitly setting a value on that field.
#GeneratedValue means that either Hibernate or your database of choice will set the entity's id(either of which depends on your GenerationType), so setting an explicit value or allowing it to be publicly set possibly will result in throwing a Hibernate-specific StaleStateException.
So you should not include id in your constructor and also remove:
public void setId(long id)
{
this.id = id;
}