简体   繁体   中英

how to insert a record in the database using apache camel jpa?

I don't understand how to use camel-jpa.

I have a entity:

@Entity
@Table(name = "task")
public class Task {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private Long id;

    @Column(name = "task_uuid", nullable = false)
    private UUID taskUuid;

    @Column(name = "status", nullable = false)
    private String status;
}

and repository

@Repository
public interface TaskRepository extends JpaRepository<Task, Long> {
}

In this class, I listen to queue_1 and convert the message to a format for transmission to queue_2

@Service
public class RoutingMessage extends RouteBuilder {

    private final TaskRepository taskRepository;

    private static final Logger log = LoggerFactory.getLogger(RoutingMessage.class);

    @Autowired
    public RoutingMessage(TaskRepository taskRepository) {
        this.taskRepository = taskRepository;
    }

    @Override
    public void configure() throws Exception {
        from("jms:{{queue1}}")
                .process(exchange -> {
                    String s = JsonUtil.convertJsonToXmlTaskEntity(String.valueOf(exchange.getIn().getBody()));
                    exchange.getIn().setBody(s);
                })
                .to(ExchangePattern.InOnly, "jms:{{queue2}}")
                .log("send to queue2");
    }
}

How do I add an insert entity(Task) to the database in the configure() method? (I use Spring boot)

For a single bean:

.to("jpa:your.package.and.entity.classname")

For a list of beans:

.to("jpa:your.package.and.entity.classname?entityType=java.util.ArrayList")

I don't know where you want to save, something like:

from("jms:{{queue1}}")
    .process(exchange -> {
        String s = JsonUtil.convertJsonToXmlTaskEntity(String.valueOf(exchange.getIn().getBody()));
        exchange.getIn().setBody(s);
    })
    .to(ExchangePattern.InOnly, "jms:{{queue2}}")
    .log("send to queue2");
    .to("jpa:your.entity.package.Task")

you can do much more with the JPA component, see docs and Github repository

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