Hibernate get data lach of column - java

I wrote a function get data from Db, it take most of the data but still lack some of column even i did define it in the entity.
Here is the result when i debug
data is null
here is my entity
#Entity
#Table(name="BANK_FEE_AND_LIMIT")
#NamedQuery(name="BankFeeAndLimit.findAll", query="SELECT b FROM
BankFeeAndLimit b")
public class BankFeeAndLimit implements Serializable {
private static final long serialVersionUID = 1L;
#Id
private long id;
#Column(name="BANK_ID")
private Long bankId;
#Column(name="CASH_IN_FEE")
private Double cashInFee;
#Column(name="CASH_IN_FEE_AT_COUNTER")
private Double cashInFeeAtCounter;
#Column(name="CASH_IN_FEE_FROM_IB")
private Double cashInFeeFromIb;
#Column(name="CASH_IN_FEE_WITHOUT_LINK")
private Double cashInFeeWithoutLink;
#Column(name="CASH_OUT_FEE")
private Double cashOutFee;
#Column(name="CASH_OUT_FEE_WITHOUT_LINK")
private Double cashOutFeeWithoutLink;
#Temporal(TemporalType.TIMESTAMP)
#Column(name="CREATE_DATE")
private Date createDate;
#Column(name="FEE_TYPE")
private Long feeType;
#Temporal(TemporalType.TIMESTAMP)
#Column(name="LAST_UPDATE")
private Date lastUpdate;
#Column(name="MAX_CASH_IN_PER_DAY")
private Long maxCashInPerDay;
#Column(name="MAX_CASH_IN_PER_TRANS")
private Long maxCashInPerTrans;
#Column(name="MAX_CASH_OUT_PER_DAY")
private Long maxCashOutPerDay;
#Column(name="MAX_CASH_OUT_PER_TRANS")
private Long maxCashOutPerTrans;
#Column(name="MAX_NUMBER_CASH_IN")
private Long maxNumberCashIn;
#Column(name="MAX_NUMBER_CASH_OUT")
private Long maxNumberCashOut;
#Column(name="MIN_CASH_IN_FEE_AT_COUNTER")
private Long minCashInFeeAtCounter;
#Column(name="MIN_CASH_IN_FEE_FROM_IB")
private Long minCashInFeeFromIb;
#Column(name="MIN_CASH_IN_FEE_VALUE")
private Long minCashInFeeValue;
#Column(name="MIN_CASH_IN_FEE_WITHOUT_LINK")
private Long minCashInFeeWithoutLink;
#Column(name="MIN_CASH_IN_PER_TRANS")
private Long minCashInPerTrans;
#Column(name="MIN_CASH_OUT_FEE_VALUE")
private Long minCashOutFeeValue;
#Column(name="MIN_CASH_OUT_FEE_WITHOUT_LINK")
private Long minCashOutFeeWithoutLink;
#Column(name="MIN_CASH_OUT_PER_TRANS")
private Long minCashOutPerTrans;
#Column(name="STATUS")
private Long status;
#Column(name="WALLET_LEVEL")
private Long walletLevel;
#Column(name="WALLET_TYPE")
private Long walletType;
#Column(name="NUMBER_IBFT_FREE_BY_MONTH")
private Long numberIbftFreeByMonth;
#Column(name="NUMBER_CASH_OUT_FREE_BY_MONTH")
private Long numberCashOutFreeByMonth;
#Column(name="MAX_NUMBER_TRANSFER_IBFT")
private Long maxNumberTransferIbft;
#Column(name="MIN_IBFT_FEE_VALUE")
private Long minIbftFeeValue;
#Column(name="MIN_CASH_IN_FEE_FOR_PREPAID")
private Long minCashInFeeForPrepaid;
#Column(name="EXTRA_TRANSFER_IBFT_FEE")
private Long extraTransferIbftFee;
#Column(name="TRANSFER_IBFT_FEE")
private Long transferIbftFee;
#Column(name="CASH_IN_FEE_FOR_PREPAID")
private Long cashInFeeForPrepaid;
#Column(name="MIN_TRANSFER_IBFT_PER_TRANS")
private Long minTransferIbftPerTrans;
#Column(name="MAX_TRANSFER_IBFT_PER_TRANS")
private Long maxTransferIbftPerTrans;
#Column(name="MAX_CASH_IN_PREPAID_PER_TRANS")
private Long maxCashInPrepaidPerTrans;
#Column(name="MAX_TRANSFER_IBFT_PER_DAY")
private Long maxTransferIbftPerDay;
#Column(name="MAX_CASH_IN_PREPAID_PER_DAY")
private Long maxCashInPrepaidPerDay;
#Column(name="MAX_NUMBER_CASH_IN_PREPAID")
private Double test;
public Double getTest() {
return test;
}
public void setTest(Double test) {
this.test = test;
}
the null field is the test field which map to the MAX_NUMBER_CASH_IN_PREPAID column in the database.
Here is the data in database
data in database
other field is still can fetch normally, only this field can't be get.

i think you need to generate sitters and getters for the Columns you have then have a constructor to set the variables from within java classes or Controllers ( spring , servlet .. )

Map be there is scale problem. Try
#Column(name="MAX_NUMBER_CASH_IN_PREPAID", precision=0)
private Double test;
Reference : JPA mapping a Java Double to a SQL Number column

Related

Hibernate Select top row for each group

Hi is there a way where we can do this via HQL??
where it returns me list of DTO instead of list of Object Class.
My sql query is
select * from readings join
(select max(signal) as signal,sender as sr ,receiver as rc from readings group by readings.sender,readings.receiver)a
on
a.signal=readings.signal and
a.sr=readings.sender and
a.rc=readings.receiver
Here is my DTO/Bean/Pojo class
public class Readings implements java.io.Serializable {
private Integer id;
private String sender;
private String major;
private String minor;
private int signal;
private BigDecimal power;
private BigDecimal temperature;
private String battery;
private String receiver;
private Date createdDatetime;..
and getters and setters ...
Which in HQL is
Criteria cr = session.createCriteria(com.XYZ..Readings.class)
.setProjection(Projections.projectionList()
.add(Projections.max("signal"))
.add(Projections.groupProperty("sender"))
.add(Projections.groupProperty("receiver")));
List<Readings> br=(List<Readings>)cr.list();
This fails at the last line, trying to cast Object to Readings class.

query.count() throws exception with Embeddable ID

I'm not able to get the total count of my query.
JPAQuery query = super.prepareJPAQuery(userAccountHasWorkgroup).where(
userAccountHasWorkgroup.workgroup.id.eq(workgroupId);
query.count();
userAccountHasWorkgrouphas an #Embeddable class as ID.
javax.ejb.EJBTransactionRolledbackException: org.hibernate.exception.DataException: Operand should contain 1 column(s)
I add more information:
#Entity
#Table(name = "UserAccount_has_Workgroup")
public class UserAccountHasWorkgroup implements java.io.Serializable {
private static final long serialVersionUID = 6537213525312531347L;
private UserAccountHasWorkgroupId id;
private UserAccount userAccountByUserAccountId;
private Privilege privilege;
private UserAccount userAccountByApprovedByUserAccountId;
private Workgroup workgroup;
private boolean approved;
private boolean lastActiveWorkgroup;
private boolean isWorkgroupReferent;
...
}
#Embeddable
public class UserAccountHasWorkgroupId implements java.io.Serializable {
private static final long serialVersionUID = 6368469866573301127L;
private long userAccountId;
private long workgroupId;
...
}
If i do:
List<UserAccountHasWorkgroup> result = query.list(userAccountHasWorkgroup);
it works, but when I try to count:
Long count = query.count();
I receive the DataException
Instead of:
query.count();
I do
query.uniqueResult(Wildcard.count.as("count"));
It seems to work properly.

spring data repository query included collection

I'm having an issue performing a custom query through the use of a spring data jpa repository.
I have a repository class implementing JPARepository<>. Everything works as expected for all of the built-in CRUD queries along with some custom queries, but doing qualification among inner collections isn't working and is returning back a full result set as though the qualification of the collection did not exist.
For example, here is a query:
public interface MessageRepository extends JpaRepository<Message, Integer> {
#Query("SELECT a FROM Message a, Message_Topic b WHERE a.systemNm = :theSystem AND a. applicationNm = :theApplication AND b.topicNm = :theTopicName AND a.insertTs BETWEEN :theStartDate AND :theEndDate AND a.expirationDt > CURRENT_TIMESTAMP")
List<Message> findMessagesByTopic(#Param("theSystem") String theSystem,
#Param("theApplication") String theApplication,
#Param("theTopicName") String theTopicName,
#Param("theStartDate") Date theStartDate,
#Param("theEndDate") Date theEndDate);
With the following JPA entities:
Message:
#Entity
public class Message implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#Column(name="message_id")
private int messageId;
#Column(name="application_nm")
private String applicationNm;
#Column(name="execution_instance_txt")
private String executionInstanceTxt;
#Temporal(TemporalType.TIMESTAMP)
#Column(name="expiration_dt")
private Date expirationDt;
#Column(name="grouping_des")
private String groupingDes;
#Temporal(TemporalType.TIMESTAMP)
#Column(name="insert_ts")
private Date insertTs;
#Column(name="message_detail_txt")
private String messageDetailTxt;
#Column(name="message_summary_txt")
private String messageSummaryTxt;
#Column(name="severity_des")
private String severityDes;
#Column(name="system_nm")
private String systemNm;
//uni-directional many-to-one association to Message_Topic
#OneToMany(fetch=FetchType.EAGER)
#JoinColumn(name="message_id", referencedColumnName="message_id")
private Set<Message_Topic> messageTopics;
Message_Topic:
#Entity
public class Message_Topic implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#Column(name="message_topic_id")
private int messageTopicId;
#Column(name="message_id", insertable=false, updatable=false)
private int messageId;
#Column(name="topic_nm")
private String topicNm;
#Column(name="topic_value_txt")
private String topicValueTxt;
This is your query:
SELECT a FROM Message a, Message_Topic b WHERE a.systemNm = :theSystem AND a. applicationNm = :theApplication AND b.topicNm = :theTopicName AND a.insertTs BETWEEN :theStartDate AND :theEndDate AND a.expirationDt > CURRENT_TIMESTAMP
Where are Message and Message_Topic joined?, If you transform this query to a native query, is possible you can detect the fault.

Trying a best solution?

I'm looking for a solution to my problem. I have 2 java class of domain. Graduacao and Aluno, in Graduacao I have an attribute "graus" that is a Collection.
In Aluno class, I have collection attribute "List graduacao".
I add the Graduacao in a JComboBox about ComboBoxModel and when user selected a Graduacao I have a JList that show the "graus" of Graduacao.
What I need is get Graduacao and graus that user choose and add to Aluno and persist after show results in a JTable with AbstractTableModel.
I'm trying this
#Entity
#Table(name="graduacao")
public class Graduacao {
#Id #GeneratedValue
private Integer id;
#NotNull #Column(unique = true)
private String graduacao;
#ElementCollection
#CollectionTable(name="graduacao_grau", joinColumns=#JoinColumn(name="id_graduacao"))
#Column(name="grau")
private List<String> graus;
//get and set
#Entity
#Table(name="aluno")
public class Aluno {
#Id #GeneratedValue
private Integer id;
//informacoes gerais
#NotNull
private String nome;
private String cpf;
private String rg;
private String nomePai;
private String nomeMae;
#Temporal(TemporalType.DATE)
private Date dtNascimento;
#Temporal(TemporalType.TIMESTAMP)
private Date dtCadastro;
private String status;
private String observacoes;
//logradouro
private String endereco;
private String bairro;
private String complemento;
private String cidade;
private String cep;
#Enumerated(EnumType.STRING)
private EstadoBrasileiro uf;
//contato
#ElementCollection
#CollectionTable(name="telefone_aluno", joinColumns=#JoinColumn(name="id_aluno"))
#Column(name="telefone")
private List<String> telefones;
private String email;
//graduacao
#OneToMany #JoinColumn(name="id_aluno")
private List<Graduacao> graduacao;
#Temporal(TemporalType.DATE)
private Date dataGraduou;
//federacao
#OneToMany #JoinColumn(name="id_federacao")
private List<Federacao> federacao;
//get and set
here the print
/** edit */
I solved the problem, here the project: http://www.4shared.com/zip/1Gbj-IZLce/project_example.html
A complete example is beyond the scope of StackOverflow. Two approaches are common, although neither is simple:
Create a custom TableModel that uses JPA queries and entities to implement the methods required by AbstractTableModel; a very simple example using JComboBox is shown here; a complete TableModel example with pagination is shown here.
Use org.jdesktop.beansbinding, shown here and mentioned here and here.
A number of ancillary links related to this topic are shown in this answer.

Java Mapping objects to objects - Moo

I have a question about Moo (https://github.com/geoffreywiseman/Moo/) that I haven't been able to solve on my own. I have this class structure:
class Middle{
private int id;
private Upper upper;
private List<Child> children;
private List<Middle> brothers;
}
class Upper{
private int id;
private String name;
private String lastname;
}
class Child{
private int id;
private String name;
}
and I want to translate them to:
class OutputMiddle{
private int id;
#Property(translation="Upper")
private OutputUpper outputUpper;
#CollectionProperty(itemTranslation = Upper.class)
private List<OutputChild> outputChildren;
private List<OutputMiddle> outputBrothers;
}
class OutputUpper{
private int id;
private String outputName;
}
class OutputChild{
private int id;
private String outputName;
}
What I don't know is:
Am I able to translate one attribute from one class to another attribute of another class?
And do the same but to a collection of objects?
And the same but to a collection of objects of the same class?
Why do I need this? Because I'm returning objects of the class "Middle" as JSON (or XML) and I need it to have an structure such as the "OutputMiddle", but I don't want to intervene the JSON after its creation and change the names of the nodes manually.
Thanks!
Try JMapper Framework, it's ease to use and require few configuration

Categories

Resources