简体   繁体   中英

Insert pojo to database with spring integration

I have a pojo that I want to insert into a database (Sql Server). When I run my code, I'm getting a "the conversion from UNKNOWN to UNKNOWN is unsupported". My question is, if my pojo and my database have everything named the same, can I just write it directly like this (or are there maybe annotations that'd let me do that)? Or do I always need a bean in here to do the mapping? I'm thinking this error might be because I'm jamming a pojo in where it doesn't belong.

In my main class I have

@Inject
TradePersistenceService dataService;

Trade trade = new Trade();
trade.setSec_id(-1);
trade.setSourceload_id(-1);
trade.setCusip("123");

dataService.insertTradeMessage(trade);

And in my spring configuration I have

<integration:channel id="InsertTradeMessageRequestChannel" />
<integration:channel id="InsertTradeMessageReplyChannel" />

<integration:gateway id="RTService"
    service-interface="com.whatever.TradePersistenceService">
    <integration:method name="insertTradeMessage"
        request-channel="InsertTradeMessageRequestChannel" reply-channel="InsertTradeMessageReplyChannel" />
</integration:gateway>

<int-jdbc:outbound-gateway request-channel="InsertTradeMessageRequestChannel"
    query="insert into MSRB_RTRS (Sec_ID, SourceLoad_ID, CUSIP) values (:Sec_ID, :SourceLoad_ID, :CUSIP"
    data-source="rmsa">
</int-jdbc:outbound-gateway>

In the database, I have a table called MSRB_RTRS with two numeric columns, and a varchar.

Finally, my pojo (with the standard getters and setters omitted here to reduce clutter)

public class Trade {
int sec_id;
int sourceload_id;
String cusip;
}

First of all you have to use update , not query , for INSERT . Second it isn't clear how that Sec_ID is related to the sec_id property in the Trade class.

You should consider to use something like:

<beans:bean id="parameterSourceFactory" class="org.springframework.integration.jdbc.ExpressionEvaluatingSqlParameterSourceFactory">
    <beans:property name="parameterExpressions">
        <beans:map>
            <beans:entry key="Sec_ID" value="payload.sec_id" />
            <beans:entry key="SourceLoad_ID" value="payload.sourceload_id" />
        </beans:map>
    </beans:property>
</beans:bean>

You can find more info in the Reference Manual :

In the example above, messages arriving on the channel labelled input have a payload of a map with key foo, so the [] operator dereferences that value from the map. The headers are also accessed as a map.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM